剑指 Offer 57 - II. 和为s的连续正数序列
class Solution {
public:
vector<vector<int>> findContinuousSequence(int sum) {
vector<vector<int>> ans;
vector<int> res;
int l = 1, r = 1, s = 1;
while (r < sum) {
while (s < sum) r++, s += r;
if (s == sum) {
res.clear();
for (int i = l; i <= r; i++)
res.push_back(i);
ans.push_back(res);
}
s -= l;
l++;
}
return ans;
}
};