当前位置:网站首页>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;
}
};边栏推荐
- Explain in detail the process of realizing Chinese text classification by CNN
- Apache iceberg source code analysis: schema evolution
- Talk about the favorite tools used by project managers
- The 13th simulation problem of the single chip microcomputer provincial competition of the Blue Bridge Cup
- t10_ Adapting to Market Participantsand Conditions
- Development cost of smart factory management system software platform
- Android development interview was badly hit in 3 years, and now the recruitment technical requirements are so high?
- Yolov5 practice: teach object detection by hand
- A database editing gadget that can edit SQLite database. SQLite database replaces fields. SQL replaces all values of a field in the database
- L'ouverture d'un compte d'actions en ligne est - elle sécurisée? Fiable?
猜你喜欢

Mujoco model learning record

The method of real-time tracking the current price of London Silver

An example of data analysis of an old swatch and an old hard disk disassembly and assembly combined with the sensor of an electromagnetic press

Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #

Penetration practice vulnhub range Keyring

Quick foundation of group theory (5): generators, Kelley graphs, orbits, cyclic graphs, and "dimensions" of groups?

MySQL connection tools

Data warehouse (3) star model and dimension modeling of data warehouse modeling

Localization through custom services in the shuttle application

主成分之综合竞争力案例分析
随机推荐
EasyCVR通过国标GB28181协议接入设备,出现设备自动拉流是什么原因?
Step size of ode45 and reltol abstol
Xia CaoJun ffmpeg 4.3 audio and video foundation to engineering application
Euler function: find the number of numbers less than or equal to N and coprime with n
主成分计算权重
Is online stock account opening safe? Is it reliable?
Subnet division and summary
Penetration practice vulnhub range Keyring
必看,时间序列分析
Oracle TRUNC function processing date format
golang中的select详解
Review Net 20th anniversary development and 51aspx growth
js如何将带有分割符的字符串转化成一个n维数组
Bug of QQ browser article comment: the commentator is wrong
D @ safety and dip1000
[PHP foundation] realize the connection between PHP and SQL database
How to retrieve the password for opening Excel files
Penetration practice vulnhub range Nemesis
Win10+vs2019 Community Edition compiling OpenSSL
The 13th simulation problem of the single chip microcomputer provincial competition of the Blue Bridge Cup