【Notes18】剑指offer_61-68题


61.扑克牌中的顺子

在这里插入图片描述

//java版
class Solution {
    
    
    public boolean isStraight(int[] nums) {
    
    
        if(nums.length != 5) return false;
        Arrays.sort(nums);// 0 0 xxx
        //去除0
        int k = 0;
        while(nums[k] == 0){
    
    //2
            k ++; //3
        }
        //nums[k] != 0
        //看有没有重复数字 如果有 false
        for(int i = k + 1; i < nums.length; i ++) 
            if(nums[i] == nums[i - 1])
                return false;
        //看除了0以外的最大 - 最小 <= 4
        return nums[4] - nums[k] <= 4;
    }
}
//C++版
class Solution {
    
    
public:
    bool IsContinuous( vector<int> nums ) {
    
    
        if(nums.empty()) return false;
        //大小王 0
        //去除0
        sort(nums.begin(), nums.end());
        int k = 0;
        while(nums[k] == 0) k ++;
        //找从k + 1位开始的  有没有重复数字
        for(int i = k + 1; i < nums.size(); i ++)
        {
    
    
            if(nums[i] == nums[i - 1])
                return false;
        }
        
        //确保最大值-最小值 <= 4
        return nums.back() - nums[k] <= 4;
    }
};

62.圆圈中最后剩下的数字

在这里插入图片描述

//java版
class Solution {
    
    
    public int lastRemaining(int n, int m) {
    
    
        //旧编号 0    1   2 ...   m-1 m m+1  ... n-1
        //新编号 -m -m+1 -m+2 ... x   0  1  ...  n-1-m
        //递推关系 f(n,m) = (f(n - 1, m) + m) % n      
        if(n == 1) return 0;
        return (lastRemaining(n - 1, m) + m) % n;
    }
}
//C++版
class Solution {
    
    
public:
    int LastRemaining_Solution(int n, int m)
    {
    
    
        if(n == 0) return -1;
        if(n == 1) return 0;
        return (LastRemaining_Solution(n - 1, m ) + m) % n;
    }
};

63.股票的最大利润

在这里插入图片描述

//java版
class Solution {
    
    
    public int maxProfit(int[] prices) {
    
    
        if(prices.length == 0)
            return 0;
        int maxv = 0;//最大利润
        int minv = prices[0];//最小价格,默认第一个价格最小
        for(int i = 1; i < prices.length; i ++){
    
    
            maxv = Math.max(prices[i] - minv, maxv);
            minv = Math.min(minv, prices[i]);
        }
        return maxv;
    }
}
//C++版
class Solution {
    
    
public:
    int maxProfit(vector<int>& nums) {
    
    
        if(nums.empty()) return 0;
        int maxv = 0;
        for(int i = 1, minv = nums[0]; i < nums.size(); i ++)
        {
    
    
            maxv = max(maxv, nums[i] - minv);
            minv = min(minv, nums[i]);
        }
        return maxv;
    }
};

64.求1+2+…+n

在这里插入图片描述

//java版
class Solution {
    
    
 int[] test=new int[]{
    
    0};
    public int sumNums(int n) {
    
    
        try{
    
    
            return test[n];
        }catch(Exception e){
    
    
            return n+sumNums(n-1);
        }
    }
}
//C++版
class Solution {
    
    
public:
    int Sum_Solution(int n) {
    
    
        int res = n;
        (n > 0) &&  (res += Sum_Solution(n - 1));
        return res;
    }
};

65.不用加减乘除做加法

在这里插入图片描述

//java版
class Solution {
    
    
    public int add(int a, int b) {
    
    
        //与  异或
        while(b != 0){
    
    //进位找到0就停止 a就是和
            int c = (a & b) << 1;//c进位
            a ^= b;//a非进位部分的和
            b = c;
        }
        return a;
    }
}
//C++版
class Solution {
    
    
public:
    int Add(int num1, int num2)
    {
    
    
        // & ^
        //^ 相同 0   不同1
        //0^0  1  1^1 0  0^ 1 1
        //利用这个性质,我们能够得到32位2进制中 不进1的位置
        //1  0^0  0^1
        //找到进1的位置
        // 1 & 1 = 1
        while(num2)
        {
    
    
            int sum = num1 ^ num2;
            int carry = (unsigned int)(num1 & num2) << 1;
            num1 = sum;
            num2 = carry;
        }
        //out = sum + carry
        return num1;

    }
};

66.构建乘积数组

在这里插入图片描述

//java版
class Solution {
    
    
    public int[] constructArr(int[] a) {
    
    
        //想办法在 i的时候 避开× ai
        int n = a.length;
        int[] res = new int[n];
        for(int i = 0,p = 1; i < n; i ++){
    
    
            res[i] = p;// a1 **** ai -1    
            p *= a[i];
        }
        for(int i = n - 1, p = 1; i >= 0; i --){
    
    
            res[i] *= p;//ai+1 **** an-1
            p *= a[i];
        }
        return res;
    }
}
//C++版
class Solution {
    
    
public:
    vector<int> multiply(const vector<int>& A) {
    
    
        if(A.empty()) return vector<int>();
        
        int n = A.size();
        vector<int> B(n);
        
        // i 左边
        for(int i = 0, p = 1; i < n; i ++)
        {
    
    
            //a1 *...* ai-1
            B[i] = p;
            p *= A[i];
        }
            
       //i右边
        //ai+1 *....* an-1
        for(int i = n - 1,p = 1; i >= 0; i --)
        {
    
    
            B[i] *= p;
            p *= A[i];
        }
        
        return B;
    }
};

67.把字符串转换成整数

在这里插入图片描述

//java版
class Solution {
    
    
    public int strToInt(String str) {
    
    
        //2147483647   214748364 + 还有一个数字 7比较大小
        //-2147483648  214748364 + 还有一个数字
        char[] c = str.trim().toCharArray();
        if(c.length == 0)
            return 0;
        
        int res = 0, sign = 1;//res返回结果 sign 1正数 -1负数
        int temp = Integer.MAX_VALUE / 10;//214748364
        int index = 1;
        if(c[0] == '-') sign = -1;
        else if(c[0] != '+') index = 0;
        for(int i = index; i < c.length; i ++){
    
    
            //"4193 with words"  4193
            //"words and 987"  0
            if(c[i] < '0' || c[i] > '9') break;
            if(res > temp || (res == temp && c[i] > '7'))
                return sign == 1   Integer.MAX_VALUE : Integer.MIN_VALUE;
            res = res * 10 + c[i] - '0';
        }
        return sign * (int)res;
    }
}
//C++版
class Solution {
    
    
public:
    int StrToInt(string str) {
    
    
        int k = 0;
        long long num = 0;
        int sign = 1;
        if(str[k] == '-')  k ++, sign = -1;
        else if(str[k] == '+')  k ++, sign = 1;
        
        while(k < str.size())
        {
    
    
            if(str[k] >= '0' && str[k] <= '9')
            {
    
    
                num  = num * 10 + str[k] - '0';
                k ++;
            }
            else{
    
    
                num = 0;
                break;
            }
        }
        num *= sign;
        return num;
    }
};

68.二叉搜索树的最近公共祖先

在这里插入图片描述

//java版
class Solution {
    
    
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    
    
        if(p.val > q.val){
    
    //确保p  < q
            TreeNode temp = p;
            p = q;
            q = temp;
        }
        while(root != null){
    
    
            if(root.val < p.val)
                root = root.right;
            else if(root.val > q.val)
                root = root.left;
            else break;
        }
        return root;
    }
}
//C++版
class Solution {
    
    
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    
    
        if(!root) return NULL;
        if(root == p || root == q) return root;
        auto left = lowestCommonAncestor(root -> left, p, q);
        auto right = lowestCommonAncestor(root -> right, p, q);
        if(left && right) return root;
        if(left) return left;
        return right;
    }
};

二叉树的最近公共祖先
在这里插入图片描述

//java版
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) return right;
        if(right == null) return left;
        return root;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43435675/article/details/107240969