当前位置:网站首页>Sum of three numbers
Sum of three numbers
2022-07-01 18:34:00 【Free dreamer】

This problem uses a double pointer Algorithm , How to use double pointers when there are three numbers , We can fix a number first i, Then double pointer j and k.
The key point of weight removal is designed in this problem :
1. Guarantee i<k<j,, Why? , Here is an example to know , for instance ,i take -1,j take 0,k take 1. If we can't meet the conditions, we can take i take -1,j take 1,k take 0. Does this repeat .
2. If i If the numbers taken twice are the same, there will be repetition
For example, the array is 1 1 1 1 1 1 -2;
here i Take the first one 1, and i Take the second 1 It's all repetitive , So we need to be i Skip directly when traversing to the same number .
Ideas :
We use double pointers in this problem , Double pointers need to be ordered , We can arrange the order in advance , then i and j All have to be de weighted , Next is the double pointer operation .
step :
1. Create an answer array
2. Loop defines the first pointer , Then repeat the operation every time ,
3. Carry out the second cycle ,j=i+1,k=nums.size()-1,j<k;j++; And carry out the weight removal operation again
4. If you can, you can ,k--, Finally, judge whether the addition of three numbers is 0, If 0 Just save it in the answer to get .
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>>res;
sort(nums.begin(),nums.end());
for(int i=0;i<nums.size();i++){
if(i&&nums[i-1]==nums[i])continue;
for(int j=i+1,k=nums.size()-1;j<k;j++){
if(j>i+1&&nums[j]==nums[j-1])continue;
while(j<k-1&&nums[i]+nums[j]+nums[k-1]>=0)k--;
if(nums[i]+nums[j]+nums[k]==0)
res.push_back({nums[i],nums[j],nums[k]});
}
}
return res;
}
};边栏推荐
- C operator overloads the query table
- Review Net 20th anniversary development and 51aspx growth
- Is the fund of futures account safe? How to open an account?
- Definition of rotation axis in mujoco
- [CF559E]Gerald and Path
- Set the style of QT property sheet control
- Penetration practice vulnhub range Keyring
- Extract the compressed package file and retrieve the password
- At present, where is the most formal and safe account opening for futures speculation? How to open a futures account?
- Subnet division and summary
猜你喜欢

Flex layout

. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes
![[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter](/img/bf/f8759195ea67832835e84c851b5e81.jpg)
[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter

PCL learning materials

Domestic spot silver should be understood

The new server is packaged with the source code of H5 mall with an operation level value of several thousand

Static timing analysis (STA) in ic/fpga design

Distributed task queue: Celery usage record

Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day

Check log4j problems using stain analysis
随机推荐
Quick foundation of group theory (5): generators, Kelley graphs, orbits, cyclic graphs, and "dimensions" of groups?
Talk about the favorite tools used by project managers
Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day
Work and leisure suggestions of old programmers
Rust language - cargo, crates io
The latest software scheme of the intelligent information management system of the armed police force
Detailed explanation of select in golang
Can hero sports go public against the wind?
Oracle TRUNC function processing date format
Cassette helicopter and alternating electric field magnetic manometer DPC
(6) VIM editor MV cat redirection standard input and output more pipe symbols head tail
Smart factory digital management system software platform
[2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
transform. Forward and vector3 Differences in the use of forward
Penetration practice vulnhub range Tornado
DRF --- response rewrite
Data warehouse (3) star model and dimension modeling of data warehouse modeling
Samba basic usage
Flex layout
Is Huishang futures a regular futures platform? Is it safe to open an account in Huishang futures?