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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值