输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数)。
序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。
示例 1:
输入:target = 9
输出:[[2,3,4],[4,5]]
示例 2:
输入:target = 15
输出:[[1,2,3,4,5],[4,5,6],[7,8]]
限制:
1 <= target <= 10^5
滑动窗口:
注意至少两个数,所以序列最左边最大为(target+1)/2,窗口范围和表示为cursum
如果cursum 小于 target , 序列右边加一个数,end++, cursum+=end
如果cursum 大于 target , 序列左边减一个数,cursum-=start,start+1
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> findContinuousSequence(int target) {
if (target < 3) {
return res;
}
int start = 1;
int end = 2;
int