function maxSum(arr) {
var thisSum = maxSum = 0;
for (var i = 0; i < arr.length; i++) {
thisSum += arr[i];
if (thisSum > maxSum) maxSum = thisSum;
else if (thisSum < 0) thisSum = 0; //如果当前子列和小于0 那么重新置0
}
return maxSum;
}
以O(n)复杂度求最大子列和
最新推荐文章于 2021-08-10 13:24:56 发布