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

// 双指针
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
};
posted @ 2020-10-09 23:47  樱风凛  阅读(130)  评论(0编辑  收藏  举报