剑指OFFER----面试题57 - II. 和为s的连续正数序列
链接:https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/
代码
class Solution {
public:
vector<vector<int>> findContinuousSequence(int target) {
vector<vector<int>> res;
for (int i = 1, j = 1, s = 1; i <= target; ++i) {
while (s < target) {
s += ++j;
}
if (s == target && j - i > 0) {
vector<int> tmp;
for (int k = i; k <= j; ++k) tmp.push_back(k);
res.push_back(tmp);
}
s -= i;
}
return res;
}
};