剑指 Offer 57 - II. 和为s的连续正数序列 js

时间:2020-10-10 17:08:08   收藏:0   阅读:18
// 双指针
var findContinuousSequence = function(target) {
    let res = []
    let left = 1
    let right = 2
    while (left < right) {
         let sum = (left + right) * (right - left + 1) / 2
         if (sum === target) {
             let start = left
             let sub = new Array(right - left + 1).fill(0).map(() => start++)
             res.push(sub)
             left++
         } else if (sum > target) {
             left++
         } else {
             right++
         }
     }
     return res
};
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!