LeetCode "Sword Finger Offer", with ideas (28)

Hello everyone, I do 方圆
n’t have it, but I am familiar with it


Sword refers to Offer 60. The number of n dice points

Insert picture description here

  • Dynamic programming
class Solution {
    
    
    public double[] twoSum(int n) {
    
    
        //总体思路就是,掷n个骰子的概率
        //为掷n-1个的点数与再掷1个骰子的点数相加
        double[] pre = {
    
    1/6d,1/6d,1/6d,1/6d,1/6d,1/6d};

        for(int i = 2;i <= n;i++){
    
    
            //第n次有如下种情况
            double[] temp = new double[5 * i + 1];
            for(int j = 0;j < pre.length;j++){
    
    
                for(int k = 0;k < 6;k++){
    
    
                    temp[j + k] += pre[j] / 6d;
                }
            }
            pre = temp;
        }

        return pre;
    }
}

Sword refers to Offer 61. Straight in poker

Insert picture description here

class Solution {
    
    
	//大小王为任意牌
    public boolean isStraight(int[] nums) {
    
    
        int king = 0;
        Arrays.sort(nums);

        for(int i = 0;i < 4;i++){
    
    
            if(nums[i] == 0) king++;
            else if(nums[i] == nums[i + 1]) return false;
        }

        return (nums[4] - nums[king]) < 5;
    }
}

The sword refers to Offer 63. The maximum profit of the stock

Insert picture description here

class Solution {
    
    
    public int maxProfit(int[] prices) {
    
    
        int minPrice = Integer.MAX_VALUE;
        int res = 0;

        for(int i = 0;i < prices.length;i++){
    
    
            if(minPrice > prices[i])
                minPrice = prices[i];
            else
                res = Math.max(res,prices[i] - minPrice);
        }

        return res;
    }
}

Sword Finger Offer 64. Find 1+2+…+n

Insert picture description here

class Solution {
    
    
    int res = 0;
    public int sumNums(int n) {
    
    
        boolean x = n > 1 && sumNums(n - 1) > 0;
        res += n;
        return res;
    }
}

Jian Zhi Offer 65. No need to add, subtract, multiply and divide

Insert picture description here

class Solution {
    
    
    public int add(int a, int b) {
    
    
        while(b != 0){
    
    
            int c = (a & b) << 1;//进位
            a ^= b;//非进位和
            b = c;
        }

        return a;
    }
}

Jian Zhi Offer 66. Build a product array

Insert picture description here

class Solution {
    
    
    public int[] constructArr(int[] a) {
    
    
        //根据规律分成左半部分和右半部分计算
        if(a.length == 0) return new int[0];

        int[] res = new int[a.length];
        res[0] = 1;
        //计算左半部分
        for(int i = 1;i < res.length;i++){
    
    
            res[i] = res[i - 1] * a[i - 1];
        }
        //计算右半部分
        int temp = 1;
        for(int j = res.length - 2;j >= 0;j--){
    
    
            temp = a[j + 1] * temp;
            res[j] *= temp;
        }

        return res;
    }
}

Sword Finger Offer 67. Convert a string to an integer

Insert picture description here
Insert picture description here

class Solution {
    
    
    public int strToInt(String str) {
    
    
        //去空格
        char[] c = str.trim().toCharArray();
        if(c.length == 0) return 0;

        int res = 0,bundry = Integer.MAX_VALUE / 10;
        //判断第一个字符的正负
        int i = 1,sigh = 1;
        if(c[0] == '-') sigh = -1;
        else if(c[0] != '+') i = 0;//不是正,说明第一个为数字,标记到第一位

        for(int j = i;j < c.length;j++){
    
    
            if(c[j] < '0' || c[j] > '9') break;
            //判断越界,重中之重
            if(res > bundry || res == bundry && c[j] > '7') 
                return sigh == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            res = res * 10 + (c[j] - '0');//这里减法转换成数字
        }

        return res * sigh;
    }
}

Jian Zhi Offer 68-I. The nearest common ancestor of the binary search tree

Insert picture description here

class Solution {
    
    
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    
    
        if(p.val < root.val && q.val < root.val)
            return lowestCommonAncestor(root.left,p,q);
        if(p.val > root.val && q.val > root.val)
            return lowestCommonAncestor(root.right,p,q);
        return root;
    }
}

Jian Zhi Offer 68-II. The nearest common ancestor of the binary tree

Insert picture description here

class Solution {
    
    
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    
    
        if(root == null || root == p || root == q)
            return root;

        TreeNode left = lowestCommonAncestor(root.left,p,q);
        TreeNode right = lowestCommonAncestor(root.right,p,q);

        if(left == null && root == null) return null;
        if(left == null) return right;
        if(right == null) return left;

        return root;
    }
}

Sword Finger Offer is over!

Guess you like

Origin blog.csdn.net/qq_46225886/article/details/107441466