LeetCode 面试题57 - II. 和为s的连续正数序列
传送门:https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/双指针
class Solution {
public:
vector<vector<int>> ans;
void add_ans(int a, int b)
{
vector<int> temp;
while (a <= b) temp.push_back(a++);
ans.push_back(temp);
}
vector<vector<int>> findContinuousSequence(int target) {
int limit = (target + 1) / 2;
int l = 1, sum = 0;
for (int r = 1 ; r <= limit; ++r)
{
sum += r;
while (sum > target) sum -= l++;
if (sum == target) add_ans(l, r);
}
return ans;
}
};
页:
[1]