如果数组是{-1 3 -1 9 4 -4}。我想输出为如何在java中查找最大序列和的子数组?
“总和为15,数组为{3 -1 9 4}。”
我有总和的代码,但如何得到这个子数组?
这里是总和
int maxSum = 0, thisSum = 0;
for(int j = 0; j < a.length; j++){
thisSum += a[ j ];
if(thisSum > maxSum){
maxSum = thisSum;
}
else if(thisSum < 0)
thisSum = 0;
}
System.out.println(maxSum);
Look [here](http://www.programcreek.com/2013/02/leetcode-maximum-subarray-java/)。 – DimaSan
当你更新'maxSum'(也可能是'maxEnd')时,保持'maxStart'(基于'j')。 –
@DimaSan,只是返回最大总和 –