现阶段的最大值等于max(前一位的最大值➕本身 , 本身)
public class Solution {
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
public int maxSubArray(int[] nums) {
// write your code here
if (nums.length == 1) {
return nums[0];
}
int len = nums.length;
Integer[] sums = new Integer[len];
sums[0] = nums[0];
int max = sums[0];
for (int i = 1; i < len; i++) {
sums[i] = Math.max(sums[i-1] + nums[i], nums[i]);
if(sums[i] > max){
max = sums[i];
}
}
return max;
}
}
Subscribe to:
Post Comments (Atom)
4. Ugly Number
1*2(t2) 2*2 3*2 (t3)1*3 2*3 3*3 (t5)1*5 2*5 3*5 1*2 2*2(t2) 3*2 1*3(t3) 2*3 3*3 (t5)1*5 2*5 3*5 Solution: public int nthUglyNumbe...
-
Given a list, rotate the list to the right by k places, where k is non-negative. Wrong solution 1: public ListNode rotateRight(ListNo...
-
if (head == null || head . next == null ) { return head; } ListNode lessDummy = new ListNode (...
-
1*2(t2) 2*2 3*2 (t3)1*3 2*3 3*3 (t5)1*5 2*5 3*5 1*2 2*2(t2) 3*2 1*3(t3) 2*3 3*3 (t5)1*5 2*5 3*5 Solution: public int nthUglyNumbe...
No comments:
Post a Comment