diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md index 0d47a197..81e0b35a 100644 --- a/problems/0053.最大子序和.md +++ b/problems/0053.最大子序和.md @@ -190,7 +190,23 @@ func maxSubArray(nums []int) int { return maxSum } ``` - +Javascript: +```Javascript +var maxSubArray = function(nums) { + let result = -Infinity + let count = 0 + for(let i = 0; i < nums.length; i++) { + count += nums[i] + if(count > result) { + result = count + } + if(count < 0) { + count = 0 + } + } + return result +}; +``` diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index db80a3e5..6fa30cde 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -143,7 +143,23 @@ Python: Go: - +Javascript: +```Javascript +var wiggleMaxLength = function(nums) { + if(nums.length <= 1) return nums.length + let result = 1 + let preDiff = 0 + let curDiff = 0 + for(let i = 0; i <= nums.length; i++) { + curDiff = nums[i + 1] - nums[i] + if((curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0)) { + result++ + preDiff = curDiff + } + } + return result +}; +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)