当前位置:网站首页>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;
}
};
边栏推荐
- Product service, operation characteristics
- Is the fund of futures account safe? How to open an account?
- Penetration practice vulnhub range Tornado
- 網上股票開戶安全嗎?是否可靠?
- Review Net 20th anniversary development and 51aspx growth
- About selenium element positioning being overwritten
- 因子分析怎么计算权重?
- Check log4j problems using stain analysis
- Leetcode 1380. Lucky numbers in the matrix (save the minimum number of each row and the maximum number of each column)
- Growing up in the competition -- (Guangyou's most handsome cub) Pikachu walking
猜你喜欢
Mujoco XML modeling
Kernel stray cat stray dog pet adoption platform H5 source code
Nearly 60% of the employees strongly support Ctrip's "3+2" working mode, and work at home for two days a week
PCL learning materials
Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"
Good looking UI mall source code has been scanned, no back door, no encryption
Xia CaoJun ffmpeg 4.3 audio and video foundation to engineering application
From comedians to NBA Zhan Huang, check the encrypted advertisements during this super bowl
Explain in detail the process of realizing Chinese text classification by CNN
随机推荐
Xia CaoJun ffmpeg 4.3 audio and video foundation to engineering application
Wechat applet blind box - docking wechat payment
Opencv map reading test -- error resolution
Classpath classpath
MySQL connection tools
Can hero sports go public against the wind?
PTA year of birth
About selenium element positioning being overwritten
Software construction scheme of smart factory collaborative management and control application system
Redis master-slave realizes 10 second check and recovery
Fix the black screen caused by iPhone system failure
Mysql database design
因子分析怎么计算权重?
Nearly 60% of the employees strongly support Ctrip's "3+2" working mode, and work at home for two days a week
Fix the problem that easycvr device video cannot be played
Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day
[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter
传感器尺寸、像素、DPI分辨率、英寸、毫米的关系
The ultimate version of the 13th simulation of the single chip microcomputer provincial competition of the Blue Bridge Cup
Leetcode 1380. Lucky numbers in the matrix (save the minimum number of each row and the maximum number of each column)