SOURCE

var maxSubArray = function(nums) {
    let max = nums[0] // 最大值从第一位开始
    for (let i = 1; i < nums.length; i++) {
        nums[i] += Math.max(nums[i - 1], 0)
        max = Math.max(nums[i], max)
    }
    return max
};
//输出6,因为连续子数组[4,-1,2,1]的和最大,为 6
console.log(maxSubArray([-9,1,-2,4,-1,2,1,-6,5]))
console 命令行工具 X clear

                    
>
console