方法一:暴力法
上界为N/2。时间复杂度为O(2N*)。空间复杂度为O(1)
方法二:滑动窗口
时间复杂度:O(N):左指针与右指针至多移动到N/2。空间复杂度O(1)
class Solution {
public:
vector<vector<int>> findContinuousSequence(int target) {
vector<vector<int>> result;
if(target<3) return {};
int left = 1,right = 2,sum=3;
while(left<right&&right<=target/2+1){
if(target>sum){
right++;
sum+=right;
}else if(target<sum){
sum-=left;
left++;
}else{
vector<int> temp;
for(int i = left;i<=right;i++){
temp.push_back(i);
}
result.push_back(temp);
sum-=left;
left++;
}
}
return result;
}
};