思路
使用滑动窗口,并维护变量sum
记录窗口内元素之和,用于与目标比较
当sum比目标小时,窗口右边界向右移动,元素增多,增大sum的值
反之移动左边界,减小sum
/**
* @param {number} target
* @return {number[][]}
*/
var findContinuousSequence = function(target) {
let res = [];
//滑动窗口左右界, 闭区间
let left = 1;
let right = 2;
let sum = left+ right; //滑动窗口内所有值的和
while(right < target/2 + 1){
if(sum === target){
res.push(range(left,right));
right++;
sum+=right;
}else if(sum < target){
right++;
sum+=right;
}else if(sum > target){
sum-=left;
left++;
}
}
return res;
};
function range(left,right){
let res = [];
for(let i=left;i<=right;i++){
res.push(i);
}
return res;
}