力扣---2020.2.26

62. 不同路径

//动态规划
class Solution {
    public int uniquePaths(int m, int n) {
        int[][] dp = new int[m][n];
        for(int i=0;i<m;i++) dp[i][0] = 1;
        for(int i=0;i<n;i++) dp[0][i] = 1;
        for(int i = 1;i<m;i++){
            for(int j = 1;j<n;j++){
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m -1][n - 1];
    }
}
class Solution {
    public int uniquePaths(int m, int n) {
        int[] cur = new int[n];
        Arrays.fill(cur,1);
        for (int i = 1; i < m;i++){
            for (int j = 1; j < n; j++){
                cur[j] += cur[j-1] ;
            }
        }
        return cur[n-1];
    }
}

378. 有序矩阵中第K小的元素

class Solution {
    public int kthSmallest(int[][] matrix, int k) {
        int[] res = new int[matrix.length*matrix[0].length];
        int n = 0;
        for(int i = 0;i<matrix.length;i++){
            for(int j = 0;j<matrix[0].length;j++){
                res[n++] = matrix[i][j];
            }
        }
        Arrays.sort(res);
        return res[k-1];
    }
}
class Solution {
    public int kthSmallest(int[][] matrix, int k) {
    int m = matrix.length, n = matrix[0].length;
    int lo = matrix[0][0], hi = matrix[m-1][n-1];
    while (lo <= hi) {
        int cnt = 0, mid = lo + (hi - lo) / 2;
        int i = m - 1, j = 0;
        while (i >= 0 && j < n) {
            if (matrix[i][j] <= mid) {
                cnt += i + 1;
                j++;
            } else i--;
        }
        if (cnt < k) lo = mid + 1;
        else hi = mid - 1;
    }
    return lo;
   }
}
public int kthSmallest(int[][] matrix, int k) {
        if(matrix == null || matrix.length == 0) {
            return 0;
        }
        int x = 0, y = matrix[0].length - 1;
        while(x < matrix.length && y >= 0) {
            int left = countSmaller(matrix, matrix[x][y]);
            int right = count(matrix, matrix[x][y]);  
            
            if(left < k && k <= right) {
                return matrix[x][y];
            }else if(right < k) {
                x++;    
            }else if(right > k) {
                y--;
            }
        }
        return 0;
    }

202. 快乐数

class Solution {
    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        set.add(n);
        while(n != 1){
            n = change(n);
            if(set.contains(n)) return false;
            set.add(n);
        }
        return true;
    }
    
    public int change(int n){
        int sum = 0;
        int num;
        while(n != 0){
            num = n%10;
            n /= 10;
            sum += num*num;
        }
        return sum;
    }
}
class Solution {
    public boolean isHappy(int n) {
        int fast=n;
        int slow=n;
        do{
            slow=squareSum(slow);
            fast=squareSum(fast);
            fast=squareSum(fast);
        }while(slow!=fast);
        if(fast==1)
            return true;
        else return false;
    }
    
    private int squareSum(int m){
        int squaresum=0;
        while(m!=0){
           squaresum+=(m%10)*(m%10);
            m/=10;
        }
        return squaresum;
    }
}
class Solution {
    public boolean isHappy(int n) {
        //这个是实际测试的,如果想要形成循环,其中循环结果有4.
        //换言之当n等于4以后则意味着进入了死循环
        if(n==4){
            return false;
        }
        int result = 0;
        while(n>0){
            result += Math.pow(n%10,2);
            n = n/10;
        }
        //如果结果不是1则递归。
        return result==1?true:isHappy(result);
    }
}

你知道的越多,你不知道的越多。
有道无术,术尚可求,有术无道,止于术。
如有其它问题,欢迎大家留言,我们一起讨论,一起学习,一起进步

相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页