当前位置:网站首页>57 - II. Continuous positive sequence with sum s
57 - II. Continuous positive sequence with sum s
2022-06-12 05:18:00 【Be your goat】
The finger of the sword Offer 57 - II. And for s Continuous positive sequence of
Ideas : The sliding window
- initialization : Left boundary i=1, Boundary j=2, Elements and s=3, Result list res
- loop : When i>=j Jump out of :
- When s>target: Move the left border to the right i=i+1, And update elements and
- When s<target: Moving to the right has a boundary j=j+1, And update elements and
- When s==target: Record integer sequence , And move the left border to the right i=i+1
- Return value :res
class Solution {
public:
vector<vector<int>> findContinuousSequence(int target) {
int i=1,j=2,sum=i+j;
vector<vector<int>> ans;
while(i<j){
if(sum<target){
j=j+1;
sum+=j;
}
else if(sum>target){
sum-=i;
i++;
}
else{
vector<int> res(j-i+1);
for(int k=0;k<j-i+1;++k){
res[k]=k+i;
}
ans.push_back(res);
sum-=i;
++i;
}
}
return ans;
}
};
Time complexity O(n) n yes target
Spatial complexity O(1)
边栏推荐
- Detailed analysis of the 2021 central China Cup Title A (color selection of mosaic tiles)
- Some optimization methods for UI Application of Qt5 on Hisilicon security platform
- How to count the total length of roads in the region and draw data histogram
- 4.3 模拟浏览器操作和页面等待(显示等待和隐式等待、句柄)
- [backtracking] backtracking method to solve combinatorial problems
- Ubunt 20.04 uses CDROM or ISO as the installation source
- 20000 word detailed reptile knowledge reserve, basic exercises of data collection and cleaning (I) reference answers to the first song
- IC验证中的force/release 学习整理(5)研究对 reg类型信号的影响
- Data processing and data set preparation
- Asp. Net core EF value conversion
猜你喜欢

2022 self study materials for Zhejiang computer level III network and security technology examination (1) (updated on 2.28)

Token based authentication

Acquisition of Lai data, NPP data, GPP data and vegetation coverage data

Image processing 13- calculation of integral diagram
![February 19, 2022 [Nolan] Nolan resurrected? Change · Nolan [soul orchid] can be connected to XDD / silly girl](/img/1a/ab2158a532683632f3a12fe41812f5.jpg)
February 19, 2022 [Nolan] Nolan resurrected? Change · Nolan [soul orchid] can be connected to XDD / silly girl

4.3 simulate browser operation and page waiting (display waiting and implicit waiting, handle)

Chrome is amazingly fast, fixing 40 vulnerabilities in less than 30 days

Longest palindrome string

Data processing and data set preparation

SQL transaction
随机推荐
Longest palindrome string
Main business objects of pupanvr record (5)
Some problems of silly girl solved
How to clear floating, and how does it work?
[backtracking method] queen n problem
Radiometric calibration and atmospheric correction of sentry 2 L1C multispectral data using sen2cor
LabVIEW about TDMS and Binary Storage Speed
How to quickly reference uview UL in uniapp, and introduce and use uviewui in uni app
Walking "daily question" and "DP"
Detailed tutorial on the use of yolov5 and training your own dataset with yolov5
Yolo opencv scale identification scale reading identification water gauge identification water level identification source code
Detailed explanation of data envelopment analysis (DEA) (taking the 8th Ningxia provincial competition as an example)
[backtracking method] backtracking method to solve the problem of Full Permutation
1006 next spread
Sword finger offer30 days re brush
Platform of ASoC framework driven by alsa
[GIS tutorial] land use transfer matrix
Link: fatal error lnk1168: cannot open debug/test Solution of exe for writing
Codec of ASoC framework driven by alsa
MySQL5.7.21 Build For ARM