class Solution {
public:
vector<vector<int> > findContinuousSequence(int sum) {
vector<vector<int> > res {};
int mid = sum+1>>1;
for (int i = 1, j = 1, s = 1; i < mid; ++i)
{
while(s < sum && j < mid) s += ++j;
if (s == sum) {
vector<int> vec (j-i+1);
generate(vec.begin(), vec.end(), [n = i] () mutable {return n++;});
res.push_back(vec);
}
s -= i;
}
return res;
}
};
class Solution {
public:
vector<vector<int> > findContinuousSequence(int sum) {
vector<vector<int>> res {};
auto f = [](int a1, int an) -> int {return (an-a1+1)*(a1+an)/2;};
auto expand = [&res] (vector<int> && vec) {res.emplace_back(vec);};
int end = sum+1>>1;
for (int i = 1; i < end; i++)
{
int l = i, r = end; int mid;
while (l < r)
{
mid = l + r >> 1;
if (f(i, mid) >= sum) r = mid;
else l = mid+1;
}
if (f(i, r) == sum)
{
vector <int> vec (r-i+1);
generate(vec.begin(), vec.end(), [n=i] () mutable {return n++;});
expand(move(vec));
}
}
return res;
}
};
class Solution {
public:
vector<vector<int> > findContinuousSequence(int sum) {
vector<vector<int> > res {};
int end = (sum+1)/2;
auto f = [](int a1, int an) -> int {return (an-a1+1)*(a1+an)/2;};
for (int start = 1; start <= end; ++start)
{
for (int i = start, j = end; i < j; )
{
cout << i << " " << j << " = " << f(i, j) << "\n";
if (f(i, j) == sum)
{
vector<int> vec(j-i+1);
generate(vec.begin(), vec.end(), [n=i] () mutable {return n++;});
res.push_back(vec);
break;
}
else if (f(i, j) > sum) --j;
else ++i;
}
}
return res;
}
};