LeetCode經典編程題(一)

1. Min depth of Binary Tree

題目描述

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Solution

idea

when the node.left==null && node.right==null, we can get the current path depth. And use this function recursively.

need to pay attention

You can’t only use Math.min(findMin(node.right,depth+1),findMin(node.left,depth+1));
because we need to judge whether the current node is a leaf.

Code

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int run(TreeNode root) {
        if(root==null)return 0;
        return findMin(root, 1);
    }
    public int findMin(TreeNode node,int depth){
        if(node.left==null&&node.right==null){
            return depth;
        }else{
            if(node.left==null){
                return findMin(node.right,depth+1);
            }else if(node.right==null){
                return findMin(node.left,depth+1);
            }else{
                return Math.min(findMin(node.right,depth+1),
                               findMin(node.left,depth+1));
            }
        }
    }
}

2. evaluate-reverse-polish-notation

題目描述

Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are+,-,*,/. Each operand may be an integer or another expression.
Some examples:

  ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
  ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

Solution

idea

Use a stack. When we meet a number, we push it into the stack. When we meet an operator, we pop two numbers and calculate the result, then push the result into the stack.

Code

import java.util.*;
public class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        int result = 0;
        for(int i = 0; i < tokens.length; i++){
            int num1 = 0,num2 = 0;
            switch(tokens[i]){
                case "+":
                    num1 = stack.pop();
                    num2 = stack.pop();
                    stack.push(num2 + num1);
                    break;
                case "-":
                    num1 = stack.pop();
                    num2 = stack.pop();
                    stack.push(num2 - num1);
                    break;
                case "*":
                    num1 = stack.pop();
                    num2 = stack.pop();
                    stack.push(num2 * num1);
                    break;
                case "/":
                    num1 = stack.pop();
                    num2 = stack.pop();
                    stack.push(num2 / num1);
                    break;
                default:
                    stack.push(Integer.parseInt(tokens[i]));
                    break;
            }
        }
        return stack.pop();
    }
}

3. max-points-on-a-line

題目描述

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

Solution

idea

Use a map to remember the slop and the number of points. The duplicate times and vertical line need considering separately.

need to pay attention
  • Double object -0.0 != 0.0
  • Duplicate points are different points. So we need to count the duplicate times.
  • Take vertical line into consideration.

Code

/**
 * Definition for a point.
 * class Point {
 *     int x;
 *     int y;
 *     Point() { x = 0; y = 0; }
 *     Point(int a, int b) { x = a; y = b; }
 * }
 */
import java.util.*;
public class Solution {
    public int maxPoints(Point[] points) {
        if(points.length<=2){
            return points.length;
        }
        int result = 2;
        for(int i = 0; i < points.length; i++){
            Map<Double,Integer> slop = new HashMap<>();
            int dup = 0;//duplicate
            int ver = 0;//vertical
            int maxi = 1;
            for(int j = i + 1; j < points.length; j++){
                int diffX = points[i].x - points[j].x;
                int diffY = points[i].y - points[j].y;
                if(diffX == 0 && diffY == 0){
                    dup++;
                }else if (diffX == 0){
                    if (ver == 0){
                        ver = 2;
                    }else {
                        ver++;
                    }
                    maxi = Math.max(maxi,ver);
                }else{
                    double theSlop = 1.0*diffY/diffX;
                    if(theSlop==-0.0)theSlop=0.0;
                    if(slop.containsKey(theSlop)){
                        slop.put(theSlop, slop.get(theSlop) + 1);
                    }else{
                        slop.put(theSlop,2);
                    }
                    maxi = Math.max(maxi, slop.get(theSlop));
                }
                //System.out.println(j+"-j-"+maxi);
            }
            result = Math.max(result, maxi + dup);
        }
        return result;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章