第22天--算法(Leetcode 53,62)


53.最大子数组和

public int maxSubArray(int[] nums) {         int res = Integer.MIN_VALUE;         int curRes = 0;         if(nums == null || nums.length == 0) {             return 0;         }         for(int i = 0;i < nums.length;i ++) {             curRes += nums[i];             res = Math.max(curRes,res);             curRes = curRes < 0 ? 0 : curRes;         }         return res;     }

62.不同路径

public int uniquePaths(int m, int n) {         int x = m + n - 2;         int y = n - 1;         long o1 = 1;         long o2 = 1;         for(int i = m,j = 1;i <= x && j <= n - 1;i ++,j ++) {             o1 *= i;             o2 *= j;             long temp = gcd(o1,o2);             o1 /= temp;             o2 /= temp;         }         return (int)o1;     }     public long gcd(long x,long y) {         return y == 0 ? x : gcd(y,x % y);     }