如果數組是{-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,只是返回最大總和 –