当前位置:网站首页>【剑指 Offer 】57 - II. 和为s的连续正数序列
【剑指 Offer 】57 - II. 和为s的连续正数序列
2022-07-03 16:29:00 【LuZhouShiLi】
剑指 Offer 57 - II. 和为s的连续正数序列
题目
输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数)。序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。
思路
- 当窗口的和小于 target 的时候,窗口的和需要增加,所以要扩大窗口,窗口的右边界向右移动
- 当窗口的和大于 target 的时候,窗口的和需要减少,所以要缩小窗口,窗口的左边界向右移动
- 当窗口的和恰好等于 target 的时候,我们需要记录此时的结果。设此时的窗口为 [i, j)[i,j),那么我们已经找到了一个 ii 开头的序列,也是唯一一个 ii 开头的序列,接下来需要找 i+1i+1 开头的序列,所以窗口的左边界要向右移动
代码
class Solution {
public:
vector<vector<int>> findContinuousSequence(int target) {
int i = 1;// 滑动窗口的左边界
int j = 1; // 滑动窗口的右边界
int sum = 0; // 滑动窗口中数字的和
vector<vector<int>> res;// 存储若干滑动窗口的结果
while(i <= target / 2)
{
// 扩大滑动窗口
if(sum < target)
{
// 右边界向右移动
sum += j;
j++;
}
else if(sum > target)
{
// 缩小滑动窗口
sum -= i;
i++;
}
else{
// 记录结果
vector<int> arr;
// 将滑动窗口中的数字全部写入数组arr中 左闭右开区间
for(int k = i; k < j; k++)
{
arr.push_back(k);
}
res.push_back(arr);
// 写入之后 继续寻找下一组结果 滑动窗口向右移动 减去i即可
sum -= i;
i++;
}
}
return res;
}
};
边栏推荐
- 架构实战营 - 第 6 期 毕业总结
- EditText request focus - EditText request focus
- Register in PHP_ Globals parameter settings
- 在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
- There are several APIs of airtest and poco that are easy to use wrong in "super". See if you have encountered them
- Golang 装饰器模式以及在NSQ中的使用
- Cocos Creator 2.x 自动打包(构建 + 编译)
- [combinatorics] combinatorial identity (sum of combinatorial identity products 1 | sum of products 1 proof | sum of combinatorial identity products 2 | sum of products 2 proof)
- [statement] about searching sogk1997 and finding many web crawler results
- Mysql 单表字段重复数据取最新一条sql语句
猜你喜欢

Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford
![SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]](/img/3b/7523eca5bbcdbba29d9b7f6e4791a5.jpg)
SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]

斑马识别成狗,AI犯错的原因被斯坦福找到了
![[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display](/img/46/c7f566f8fd46d383b055582d680bb7.png)
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
![[list to map] collectors Tomap syntax sharing (case practice)](/img/ac/e02deb1cb237806d357a88fb812852.jpg)
[list to map] collectors Tomap syntax sharing (case practice)

爱可可AI前沿推介(7.3)

Cocos Creator 2.x 自动打包(构建 + 编译)

面试官:JVM如何分配和回收堆外内存

Threejs Part 2: vertex concept, geometry structure

Google Earth engine (GEE) - daymet v4: daily surface weather data set (1000m resolution) including data acquisition methods for each day
随机推荐
初试scikit-learn库
Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
[combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
A survey of state of the art on visual slam
Cocos Creator 2. X automatic packaging (build + compile)
Is it safe to open an account with flush?
高等数学(第七版)同济大学 习题2-1 个人解答
AcWing 第58 场周赛
2022爱分析· 国央企数字化厂商全景报告
Golang anonymous function use
[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix
Cocos Creator 2.x 自动打包(构建 + 编译)
How to use AAB to APK and APK to AAB of Google play apps on the shelves
Data driving of appium framework for mobile terminal automated testing
Remote file contains actual operation
远程办公之大家一同实现合作编辑资料和开发文档 | 社区征文
香港理工大学|数据高效的强化学习和网络流量动态的自适应最优周界控制
The difference between calling by value and simulating calling by reference
关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM
斑马识别成狗,AI犯错的原因被斯坦福找到了