当前位置:网站首页>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;
}
};边栏推荐
- Computer network interview assault
- On the language internationalization of Yongzhong Office
- Definition of rotation axis in mujoco
- Basic usage of shell script
- Bernoulli distribution (a discrete distribution)
- Kernel stray cat stray dog pet adoption platform H5 source code
- Detailed explanation of ArrayList expansion
- [noip2015] jumping stone
- C operator overloads the query table
- Domestic spot silver should be understood
猜你喜欢

Check log4j problems using stain analysis

Good looking UI mall source code has been scanned, no back door, no encryption

What are the legal risks of NFT brought by stars such as curry and O'Neill?

Review Net 20th anniversary development and 51aspx growth

Bug of QQ browser article comment: the commentator is wrong

Common design parameters of solid rocket motor

Flex layout

主成分计算权重
![[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

. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes
随机推荐
Single element of an ordered array
主成分之综合竞争力案例分析
Nearly 60% of the employees strongly support Ctrip's "3+2" working mode, and work at home for two days a week
Blackwich: the roadmap of decarbonization is the first step to realize the equitable energy transformation in Asia
MFC obtains local IP (used more in network communication)
Yolov5 practice: teach object detection by hand
. 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
Product service, operation characteristics
徽商期货是正规期货平台吗?在徽商期货开户安全吗?
Xia CaoJun ffmpeg 4.3 audio and video foundation to engineering application
Rotation order and universal lock of unity panel
Develop those things: add playback address authentication to easycvr platform
Blue Bridge Cup real question: score statistics
SCP -i private key usage
MES production equipment manufacturing execution system software
Three dimensional anti-terrorism Simulation Drill deduction training system software
Equipment simulation and deduction training system software
Is the fund of futures account safe? How to open an account?
How to retrieve the password for opening Excel files