用两个指针,如果和>sum,l++,如果和<sum,r++
class Solution {
public:
vector<vector<int>> FindContinuousSequence(int sum) {
vector<vector<int>> result;
if (sum < 3)
return result;
int s = 3; //s=[l.....r]区间内的和
int l = 1, r = 2;
while (l < sum&&r <sum)
{
if (s == sum)
{
vector<int> res;
for (int i = l; i <= r; i++)
res.push_back(i);
result.push_back(res);
r++;
s = s + r;
}
else if (s < sum)
{
r++;
s = s + r;
}
else
{
s = s - l;
l++;
}
}
return result;
}
};