当前位置:网站首页>[Jianzhi offer] 57 - ii Continuous positive sequence with sum s
[Jianzhi offer] 57 - ii Continuous positive sequence with sum s
2022-07-03 16:32:00 【LuZhouShiLi】
The finger of the sword Offer 57 - II. And for s Continuous positive sequence of
subject
Enter a positive integer target , Output all and as target A sequence of continuous positive integers ( Contains at least two numbers ). The numbers in the sequence are arranged from small to large , The different sequences are arranged in descending order according to the first number .
Ideas
- When the sum of windows is less than target When , The sum of windows needs to be increased , So expand the window , The right border of the window moves to the right
- When the sum of windows is greater than target When , The sum of windows needs to be reduced , So we need to narrow the window , The left edge of the window moves to the right
- When the sum of windows is exactly equal to target When , We need to record the results at this point . Let the window be [i, j)[i,j), So we've found one ii The sequence at the beginning , And the only one ii The sequence at the beginning , Next we need to find i+1i+1 The sequence at the beginning , So the left side of the window moves to the right
Code
class Solution {
public:
vector<vector<int>> findContinuousSequence(int target) {
int i = 1;// Slide the left border of the window
int j = 1; // Slide the right border of the window
int sum = 0; // The sum of the numbers in the sliding window
vector<vector<int>> res;// Store the results of several sliding windows
while(i <= target / 2)
{
// Expand the sliding window
if(sum < target)
{
// The right border moves to the right
sum += j;
j++;
}
else if(sum > target)
{
// Reduce the sliding window
sum -= i;
i++;
}
else{
// Records of the results
vector<int> arr;
// Write all the numbers in the sliding window into the array arr in Left closed right open interval
for(int k = i; k < j; k++)
{
arr.push_back(k);
}
res.push_back(arr);
// After writing Continue to find the next set of results Slide the window to the right subtract i that will do
sum -= i;
i++;
}
}
return res;
}
};
边栏推荐
- 为抵制 7-Zip,列出 “三宗罪” ?网友:“第3个才是重点吧?”
- [combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
- First knowledge of database
- 【LeetCode】94. Middle order traversal of binary tree
- Qt插件之自定义插件构建和使用
- Processing strategy of message queue message loss and repeated message sending
- 拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
- 架构实战营 - 第 6 期 毕业总结
- 面试之 top k问题
- What is the maximum number of concurrent TCP connections for a server? 65535?
猜你喜欢

Thread pool executes scheduled tasks

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线

Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)

Basis of target detection (IOU)

First knowledge of database

Netease UI automation test exploration: airtest+poco

PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug

Stm32f103c8t6 firmware library lighting

爱可可AI前沿推介(7.3)

Data driving of appium framework for mobile terminal automated testing
随机推荐
TCP擁塞控制詳解 | 3. 設計空間
面试之 top k问题
【LeetCode】94. Middle order traversal of binary tree
[combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
斑马识别成狗,AI犯错的原因被斯坦福找到了
面试官:JVM如何分配和回收堆外内存
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
TCP拥塞控制详解 | 3. 设计空间
Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
[combinatorics] combinatorial identities (review of eight combinatorial identities | product of combinatorial identities 1 | proof | use scenario | general method for finding combinatorial numbers)
0214-27100 a day with little fluctuation
什么是质押池,如何进行质押呢?
Colab works with Google cloud disk
手机注册股票开户安全吗 开户需要钱吗
程序猿如何快速成长
Cocos Creator 2. X automatic packaging (build + compile)
Netease UI automation test exploration: airtest+poco
探索Cassandra的去中心化分布式架构
Remote file contains actual operation