Max subarray
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
.
A[i]有两种选择
1.新开一个数组;
2.加入原有数组max +a[i];
public class Solution { public int maxSubArray(int[] A) { int max_here=A[0]; int max_sofar=A[0]; for(int i=1;i<A.length;i++){ max_here=Math.max(max_here+A[i],A[i]); max_sofar=Math.max(max_here, max_sofar); } return max_sofar; } }