思路:利用双指针滑动窗口的思想。 窗口的左边left指向1,右边right指向2.
接下来判断窗口之间的数值和的大小与sum大小关系
当left < high的时候
如果current == sum ,得到一个序列,并且left++
如果current < sum则 right++
如果current > sum 则low++
class Solution {
public:
vector<vector<int> > FindContinuousSequence(int sum)
{
vector<vector<int> >res;
if(sum <= 0)
return res;
int low = 1, high = 2;
while(low < high)
{
int current = (((high - low +1)*(low + high))>>1);
if(current == sum)
{
vector<int>temp;
for(int i = low ; i <= high; i++)
{
temp.push_back (i);
}
res.push_back (temp);
low++;
}
else if(current < sum)
high++;
else if(current > sum)
low++;
}
return res;
}
};