剑指 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
};

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

上一篇:bzoj2208:[Jsoi2010]连通数


下一篇:js日志输出还是只会console.log么,那你就out了