标签:
1 题目:
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4]
,
the contiguous subarray [4,−1,2,1]
has the largest sum = 6
.
Divide and Conquer Array Dynamic Programming
maxSum = Math.max(maxSum, curSum);
改为if语句,存开始序号和子串长度。
public int maxSubArray(int[] A){ if (A.length == 0) { return 0; } int maxSum = A[0]; int curSum = A[0]; int len = A.length; for (int i = 1; i < len; i++) { curSum = Math.max(curSum + A[i], A[i]); maxSum = Math.max(maxSum, curSum); } return maxSum; }
标签:
原文地址:http://www.cnblogs.com/lingtingvfengsheng/p/4388324.html