public static int maxSubSum4(int[] arr){
int maxSum = 0;
int thisSum = 0;
for(int j = 0;j<arr.length;j++){
thisSum += arr[j];
//这段代码的核心:逐渐求和,若后面的数比前面的数大,便更新
//若加呀加的,发现thisSum变为负数了(在没加这个可恶的负数之前,thisSum确实是最大的),说明当前的这个负数不要
//使得 thisSum = 0;,重新进行计算
if(thisSum > maxSum){
maxSum = thisSum;
}else if(thisSum < 0){
thisSum = 0;
}
}
return maxSum;
}
最大子列和问题
最新推荐文章于 2023-12-19 22:17:39 发布