当前位置:网站首页>[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;
}
};
边栏推荐
- Golang anonymous function use
- nifi从入门到实战(保姆级教程)——flow
- 近视:摘镜or配镜?这些问题必须先了解清楚
- [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)
- Is it safe to open a stock account by mobile registration? Does it need money to open an account
- 1287. Elements that appear more than 25% in an ordered array
- Mb10m-asemi rectifier bridge mb10m
- Thinking about telecommuting under the background of normalization of epidemic | community essay solicitation
- 8个酷炫可视化图表,快速写出老板爱看的可视化分析报告
- Thread pool executes scheduled tasks
猜你喜欢

Unreal_DataTable 实现Id自增与设置RowName

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

Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs

Basis of target detection (IOU)

一台服务器最大并发 tcp 连接数多少?65535?

Mb10m-asemi rectifier bridge mb10m

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

Mysql 单表字段重复数据取最新一条sql语句
![[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix](/img/d6/3c21c25f1c750f17aeb871124e80f4.png)
[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix

Visual SLAM algorithms: a survey from 2010 to 2016
随机推荐
Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
[combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
[combinatorics] combinatorial identities (review of eight combinatorial identities | product of combinatorial identities 1 | proof | use scenario | general method for finding combinatorial numbers)
PHP secondary domain name session sharing scheme
First knowledge of database
Unreal_ Datatable implements ID self increment and sets rowname
在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford
Record windows10 installation tensorflow-gpu2.4.0
Pychart error updating package list: connect timed out
Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
疫情常态化大背景下,关于远程办公的思考|社区征文
Thinking about telecommuting under the background of normalization of epidemic | community essay solicitation
Hibernate的缓存机制/会话级缓存机制
How programming apes grow rapidly
Mongodb installation and basic operation
Acwing game 58
Visual SLAM algorithms: a survey from 2010 to 2016
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
香港理工大学|数据高效的强化学习和网络流量动态的自适应最优周界控制