当前位置:网站首页>【剑指 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;
}
};
边栏推荐
- "Everyday Mathematics" serial 56: February 25
- [combinatorics] combinatorial identities (review of eight combinatorial identities | product of combinatorial identities 1 | proof | use scenario | general method for finding combinatorial numbers)
- 跟我学企业级flutter项目:简化框架demo参考
- NSQ源码安装运行过程
- 于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
- Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN
- From the 18th line to the first line, the new story of the network security industry
- Explore Netease's large-scale automated testing solutions see here see here
- 架构实战营 - 第 6 期 毕业总结
- 消息队列消息丢失和消息重复发送的处理策略
猜你喜欢
[list to map] collectors Tomap syntax sharing (case practice)
NSQ源码安装运行过程
Remote file contains actual operation
(补)双指针专题
From the 18th line to the first line, the new story of the network security industry
Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
初试scikit-learn库
Cocos Creator 2.x 自动打包(构建 + 编译)
SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
The difference between calling by value and simulating calling by reference
随机推荐
Getting started with Message Oriented Middleware
pycharm错Error updating package list: connect timed out
Deep understanding of grouping sets statements in SQL
什么是质押池,如何进行质押呢?
《天天数学》连载56:二月二十五日
(补)双指针专题
From the 18th line to the first line, the new story of the network security industry
TCP拥塞控制详解 | 3. 设计空间
nifi从入门到实战(保姆级教程)——flow
Client does not support authentication protocol requested by server; consider upgrading MySQL client
[combinatorics] combinatorial identity (sum of variable upper terms 1 combinatorial identity | summary of three combinatorial identity proof methods | proof of sum of variable upper terms 1 combinator
Eleven requirements for test management post
Nine ways to define methods in scala- Nine ways to define a method in Scala?
疫情常态化大背景下,关于远程办公的思考|社区征文
Remote file contains actual operation
Register in PHP_ Globals parameter settings
Golang decorator mode and its use in NSQ
2022爱分析· 国央企数字化厂商全景报告
EditText request focus - EditText request focus
Colab works with Google cloud disk