题目:
class Solution {
public:
vector<vector<int>> findContinuousSequence(int target) { //本题使用滑动窗口(双指针)
int i=1, j=1; //定义左右边界,一般是左闭右开
int sum=0; //窗口内的和
vector<vector<int>> result;
while(i<=target/2){ //左边界最大为target/2
if(sum<target){ //sum小于target,加上j后右边界右移
sum+=j;
j++;
}
else if(sum>target){ //sum超过target,减去i后左边界右移
sum-=i;
i++;
}
else{
vector<int> vec; //sum等于target,记录序列
for(int k=i;k<j;k++){
vec.push_back(k);
}
result.push_back(vec);
sum-=i; //记录后左边界右移
i++;
}
}
return result;
}
};
作者:nettee
链接:https://leetcode.cn/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/solutions/133768/shi-yao-shi-hua-dong-chuang-kou-yi-ji-ru-he-yong-h/
来源:力扣(LeetCode)