力扣---2020.6.4

238. 除自身以外数组的乘积

class Solution {
    public int[] productExceptSelf(int[] nums) {
        int len = nums.length;
        int[] B = new int[len];
        for(int i = 0,product=1;i<len;i++){
            B[i] = product;
            product *= nums[i];
        }

        for(int i = len-1,product = 1;i>0;i--){
            product *= nums[i];
            B[i-1] *= product;
        }
        return B;
    }
}
class Solution {
    public int[] productExceptSelf(int[] nums) {
        int len = nums.length;
        int[] res = new int[len];
        for(int i = 0,product = 1;i < len;i++){
            res[i] = product;
            product *= nums[i];
        }

        for(int i = len-1,product = 1;i >= 0;i--){
            res[i] *= product;
            product *= nums[i];
        }
        return res;
    }
}

面试题 17.06. 2出现的次数

  • 解题思路:https://leetcode-cn.com/problems/number-of-digit-one/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-50/
class Solution {
    public int numberOf2sInRange(int n) {
        int ans=0;
        for(long i=1;i<=n;i*=10){
            int temp=(int) (n/i);
            int cur=temp%10;
            int temp1=temp/10;
            int temp2=(int) (n%i);
            ans+=(int) (temp1*i);
            if(cur==2) ans+=temp2+1;
            if(cur>2) ans+=i;
        }
        return ans;
    }
}
class Solution {
    public int numberOf2sInRange(int n) {
        int ans = 0;
        for(int i = 1;i <= n;i *= 10){
            int temp = n/i;
            int hight = temp/10; // 高位部分
            int cur = temp % 10; //当前位
            int low = n%i; // 低位部分

            if(cur < 2) ans += hight*i;
            if(cur == 2) ans += hight*i + low+1;
            if(cur > 2) ans += hight*i + i;
        }
        return ans;
    }
}
class Solution {
    public int numberOf2sInRange(int n) {
        if(n == 0) {
            return 0;
        }
        int digit = (int)Math.log10(n) + 1;
        int[][] dp = new int[digit+1][2];  
        // dp[i][0] = numberOf2sInRange(n % pow(10, i)) 保存0~n的1-i位组成的数包含2的个数
        // dp[i][1] = numberOf2sInRange(99..9) 保存i位均为9包含2的个数
        dp[1][0] = n % 10 >= 2 ? 1:0;
        dp[1][1] = 1;
        for(int i = 2; i <= digit; i++) {
            int k = n/ ((int)Math.pow(10, i-1)) % 10;
            dp[i][0] = k * dp[i-1][1] + dp[i-1][0];
            if(k == 2) {
                dp[i][0] += n % (int)Math.pow(10, i-1) +1; 
            } else if(k > 2){
                dp[i][0] +=  (int)Math.pow(10, i-1);
            }
            dp[i][1] = 10 * dp[i-1][1] + (int)Math.pow(10, i-1); //计算1-i位均为9的值包含2的个数
        }
        return dp[digit][0];
    }
}

面试题 10.09. 排序矩阵查找

//从右上角开始搜索
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0) {
            return false;
        }
        int m = matrix.length;
        int n = matrix[0].length;
        int row = 0;
        int col = n-1;
        while(row<m && col>=0){
            if(matrix[row][col]>target){
                col--;
            }else if(matrix[row][col]<target){
                row++;
            }else{
                return true;
            }
        }
        return false;
    }
}
//左下角开始搜索
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0) {
            return false;
        }
        int m = matrix.length;
        int n = matrix[0].length;
        int row = m-1;
        int col = 0;
        while(row>=0 && col<n){
            if(matrix[row][col]>target){
                row--;
            }else if(matrix[row][col]<target){
                col++;
            }else{
                return true;
            }
        }
        return false;
    }
}

你知道的越多,你不知道的越多。

你可能感兴趣的:(数据结构与算法)