add: 最大子序和

This commit is contained in:
2020-05-03 19:11:29 +08:00
parent 801073e385
commit e9fd76de1b
3 changed files with 23 additions and 0 deletions

View File

@ -0,0 +1,11 @@
/**
* @param {number[]} nums
* @return {number}
*/
export const maxSubArray = function (nums) {
let max = nums[0]
let tmp = 0
nums.forEach(n => { max = Math.max(tmp > 0 ? tmp += n : tmp = n, max) })
// 如果当前指针所指元素之前的和小于0则丢弃当前元素之前的数列
return max
}