- 输出所有和为S的连续正数序列。序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序
class Solution {
public:
vector<vector<int> > FindContinuousSequence(int sum) {
int half = sum / 2;
int prev = 1, curr = 1;
int currSum = 1;
vector<vector<int> > res;
while(curr <= half){
while(curr <= half && currSum < sum){
curr++;
currSum += curr;
}
while(prev < curr && currSum > sum){
currSum -= prev;
prev++;
}
if(currSum == sum){
vector<int> vec;
for(int i = prev; i <= curr; ++i)
vec.push_back(i);
res.push_back(vec);
currSum -= prev;
prev++;
}
}
return res;
}
};