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
.
最大子序列问题,DP算法解决:
public class Solution { public int Max(int a,int b) { return a>b?a:b; } public int maxSubArray(int[] A) { int size = A.length; int local = A[0];//局部最大值 int globe = A[0];//全局最大值 for(int i=1;i<size;i++) { local = Max(A[i],local+A[i]); globe = Max(local,globe); } return globe; } }