Open Tcdian opened 4 years ago
/**
* @param {number} target
* @return {number[][]}
*/
var findContinuousSequence = function(target) {
const n = Math.ceil(target / 2);
let i = 1;
let j = 2;
let sum = 3;
const result = [];
while (j <= n) {
if (sum < target) {
j++;
sum += j;
} else if (sum > target) {
sum -= i;
i++;
} else {
result.push(Array.from(new Array(j - i + 1), (v, index) => i + index));
sum -= i;
i++;
j++;
sum += j;
}
}
return result;
};
剑指 Offer 57 - II. 和为s的连续正数序列
输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数)。
序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。
Example 1
Example 2
Note
1 <= target <= 10^5