力扣---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);
    }
}

你知道的越多,你不知道的越多。
有道無術,術尚可求,有術無道,止於術。
如有其它問題,歡迎大家留言,我們一起討論,一起學習,一起進步

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章