当前位置:网站首页>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;
}
};边栏推荐
- Mysql database design
- Apache iceberg source code analysis: schema evolution
- ZABBIX alarm execute remote command
- When the fixed frequency artifact falls in love with multithreading | ros2 fixed frequency topic release demo
- Slider verification code identification gadget display
- Good looking UI mall source code has been scanned, no back door, no encryption
- 开发那些事儿:EasyCVR平台添加播放地址鉴权
- Explain in detail the process of realizing Chinese text classification by CNN
- 必看,时间序列分析
- Draw drawing process of UI drawing process
猜你喜欢

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

. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes

Computer network interview assault

Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion

主成分之综合竞争力案例分析

Localization through custom services in the shuttle application

Cassette helicopter and alternating electric field magnetic manometer DPC

This is the latest opportunity of the London bank trend

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

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
随机推荐
Pytorch crossentropyloss learning
[noip2015] jumping stone
Terms related to K line
Samba basic usage
Growing up in the competition -- (Guangyou's most handsome cub) Pikachu walking
Work and leisure suggestions of old programmers
Relationship between sensor size, pixel, dpi resolution, inch and millimeter
Development cost of smart factory management system software platform
LeetCode 148. Sort linked list
Operating system interview assault
Flex layout
The new server is packaged with the source code of H5 mall with an operation level value of several thousand
DRF --- response rewrite
Product service, operation characteristics
Record 3 - the state machine realizes key control and measures the number of external pulses
[today in history] February 15: Pascal's father was born; YouTube was founded; Kotlin language comes out
Htt [ripro network disk link detection plug-in] currently supports four common network disks
JS how to convert a string with a delimiter into an n-dimensional array
Yolov5 practice: teach object detection by hand
PCL learning materials