当前位置:网站首页>[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;
}
};
边栏推荐
- Cocos Creator 2.x 自动打包(构建 + 编译)
- Golang 匿名函数使用
- Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
- AcWing 第58 场周赛
- 疫情常态化大背景下,关于远程办公的思考|社区征文
- Expression of request header in different countries and languages
- 【剑指 Offer】58 - II. 左旋转字符串
- 远程办公之大家一同实现合作编辑资料和开发文档 | 社区征文
- (Supplement) double pointer topic
- Golang anonymous function use
猜你喜欢
爱可可AI前沿推介(7.3)
Google Earth engine (GEE) - daymet v4: daily surface weather data set (1000m resolution) including data acquisition methods for each day
(补)双指针专题
深入理解 SQL 中的 Grouping Sets 语句
Basis of target detection (IOU)
One article takes you to understand machine learning
Explore Netease's large-scale automated testing solutions see here see here
Remote file contains actual operation
The mixlab editing team is recruiting teammates~~
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (III)
随机推荐
Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
Cocos Creator 2. X automatic packaging (build + compile)
Data driving of appium framework for mobile terminal automated testing
TCP擁塞控制詳解 | 3. 設計空間
中南大学|通过探索理解: 发现具有深度强化学习的可解释特征
Hibernate的缓存机制/会话级缓存机制
深入理解 SQL 中的 Grouping Sets 语句
Basis of target detection (IOU)
Cocos Creator 2.x 自动打包(构建 + 编译)
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
【剑指 Offer 】57 - II. 和为s的连续正数序列
Chinese translation of Tagore's floating birds (1~10)
Is it safe to open an account with flush?
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
Nifi from introduction to practice (nanny level tutorial) - flow
NSQ源码安装运行过程
《天天数学》连载56:二月二十五日
Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
【剑指 Offer】58 - I. 翻转单词顺序
【声明】关于检索SogK1997而找到诸多网页爬虫结果这件事