题解 | #和为S的连续正数序列# 基于滑动窗口
和为S的连续正数序列
https://www.nowcoder.com/practice/c451a3fd84b64cb19485dad758a55ebe
#include <vector> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param sum int整型 * @return int整型vector<vector<>> */ vector<vector<int>> FindContinuousSequence(int sum) { // write code here vector<int> tmp(sum/2+1,0); vector<vector<int>> res; if(sum == 1) return res; for(int i=0 ; i<sum/2+1; i++){ tmp[i] = i+1; } int slow = 0,fast = 0, s = 0; while(fast <= sum/2+1){ if(s < sum){ s += tmp[fast++]; }else if(s > sum){ s -= tmp[slow++]; }else if(s == sum){ res.push_back(vector<int>{tmp.begin()+slow, tmp.begin()+fast}); s += tmp[fast++]; s -= tmp[slow++]; } } return res; } };