varmaxSubArray=function(nums){var sum =0var max = nums[0]for(num of nums){if(sum>=0){
sum+=num
}else{
sum = num
}
max = Math.max(max,sum)}return max
};
第一种方法sum用来存到达数组相应位置时最大的连续子数组和只有sum大于0时才会对后边的和有正向的增益,如果小于零就不要了var maxSubArray = function(nums) { var sum = 0 var max = nums[0] for(num of nums){ if(sum>=0){ sum+=num }else{ sum = num }