LeetCode "Sword Finger Offer"、アイデアあり(28)

みなさん、こんにちは。私は方圆
持ってませんが、慣れています


ソードはオファー60を指します。n個のダイスポイントの数

ここに画像の説明を挿入

  • 動的プログラミング
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;
    }
}

ソードはオファー61を指します。ポーカーのストレート

ここに画像の説明を挿入

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;
    }
}

剣はオファー63を指します。株式の最大利益

ここに画像の説明を挿入

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;
    }
}

ソードフィンガーオファー64。1+ 2 +…+ nを検索

ここに画像の説明を挿入

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

建志オファー65。加算、減算、乗算、および除算の必要はありません

ここに画像の説明を挿入

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.製品アレイを構築する

ここに画像の説明を挿入

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.文字列を整数に変換します

ここに画像の説明を挿入
ここに画像の説明を挿入

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。二分探索木の最も近い共通の祖先

ここに画像の説明を挿入

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。二分木の最も近い共通の祖先

ここに画像の説明を挿入

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;
    }
}

ソードフィンガーオファー終了!

おすすめ

転載: blog.csdn.net/qq_46225886/article/details/107441466