当前位置:网站首页>[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;
}
};
边栏推荐
- Google Earth engine (GEE) - daymet v4: daily surface weather data set (1000m resolution) including data acquisition methods for each day
- [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
- Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
- Central South University | through exploration and understanding: find interpretable features with deep reinforcement learning
- Mixlab编辑团队招募队友啦~~
- There are several APIs of airtest and poco that are easy to use wrong in "super". See if you have encountered them
- Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
- 利用MySQL中的乐观锁和悲观锁实现分布式锁
- 程序猿如何快速成长
- Golang anonymous function use
猜你喜欢

记一次jar包冲突解决过程

Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN

Visual SLAM algorithms: a survey from 2010 to 2016

(补)双指针专题

(Supplement) double pointer topic

什么是质押池,如何进行质押呢?

Thread pool executes scheduled tasks
![[combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi](/img/81/59ed6bebf5d85e9eb71bd4ca261309.jpg)
[combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi

Interviewer: how does the JVM allocate and recycle off heap memory

Explore Netease's large-scale automated testing solutions see here see here
随机推荐
Explore Netease's large-scale automated testing solutions see here see here
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
特征多项式与常系数齐次线性递推
AcWing 第58 场周赛
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
Top k questions of interview
Record windows10 installation tensorflow-gpu2.4.0
One article takes you to understand machine learning
Aike AI frontier promotion (7.3)
PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
面试之 top k问题
[combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
Nifi from introduction to practice (nanny level tutorial) - flow
MySQL converts comma separated attribute field data from column to row
[statement] about searching sogk1997 and finding many web crawler results
用通达信炒股开户安全吗?
Mixlab编辑团队招募队友啦~~
Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
PHP CI (CodeIgniter) log level setting