当前位置:网站首页>LeetCode 15:三数之和
LeetCode 15:三数之和
2022-07-25 05:19:00 【斯沃福德】
题目:
思路:排序 + 双指针
nSum 系列问题的核心思路就是排序 + 双指针 ;
为什么两数之和不能用双指针?
双指针需要数组排序,而两数之和需要返回的是索引,排序后索引就乱了 !
双指针思想:
根据sum的大小调整左右指针 ;
但是,这样实现会造成重复的结果,比如说 nums = [1,1,1,2,2,3,3], target = 4,得到的结果中 [1,3] 肯定会重复;
所以当nums[left]与下一个数nums[left+1]相等则left++跳过,right同理!
注意:
三数之和时,left必须从i+1开始,否则双指针时会重复使用num[i] !
Java实现:
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
// 返回的是数而不是索引 !
List<List<Integer>> r=new ArrayList<>();
if(nums.length==0){
return r;
}
// 1.先排序
Arrays.sort(nums);
for(int i=0;i<nums.length;i++){
// 去重1
if(i>0 && nums[i]==nums[i-1]){
continue; // 遍历下一个 i
}
// 如果i>0,而数组又是递增的,则注定无法凑出0 !
if(nums[i]>0){
continue;
}
// 2.双指针找为0-nums[i]的两个数,
int target=0-nums[i];
int left=i+1; // left必须从i+1开始,否则会重复使用num[i] !
int right=nums.length-1;
while(left<right){
int sum=nums[left]+nums[right];
// 根据sum来调整指针
if(sum>target){
right--;
}else if(sum<target){
left++;
}else if(sum==target){
r.add(Arrays.asList(nums[i],nums[left],nums[right]));
// 去重2:第一次添加到List后就要去重!
while(left<right && nums[left]==nums[left+1]){
left++;
}
while(left<right && nums[right]==nums[right-1]){
right--;
}
// 继续寻找
right--;
left++;
}
}
}
return r;
}
}
边栏推荐
- [untitled]
- nacos中哪边有这个列的sql脚本啊?
- Add click event to unity 3D object
- STL notes (III): input / output stream
- Pikachu vulnerability platform exercise
- ThreadLocal
- Necessary skills for mobile terminal test: ADB command and packet capturing
- Execution process of NPM run XX
- 300. Longest increasing subsequence
- odoo14 | 关于状态栏statusbar关键词使用后显示异常及解决方法
猜你喜欢

Dragon Dragon community released the first Anolis OS Security Guide to escort users' business systems

rhce第一天

What content does the software test plan include and how to write it. Share test plan template
搭建私有CA服务器

自己实现is_class

Teach you three ways to optimize the performance from 20s to 500ms
![[no title] 1](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[no title] 1
[email protected] R & D effectiveness measurement indicators"/>Learning records [email protected] R & D effectiveness measurement indicators

初步了解Panda3d粒子系统

JWT(json web token)
随机推荐
STM32 Development Notes 121: Kalman filter I understand
Delivery practice of private PAAS platform based on cloud native
Purpose of setting novice task period in the integral system
一篇文章带你读懂Redis的哨兵模式
What should testers do if they encounter a bug that is difficult to reproduce?
Blog Description & message board
Performance Optimization: how to solve the slow loading speed of the first screen of spa single page application?
Shenzhen on call test, subject 4 on call test, subject 3 theory, second theory on call test instructions
Which side of Nacos has the SQL script of this column?
教你三招从让性能从20s优化到500ms
1310_ Implementation analysis of a printf
Docker builds MySQL master-slave replication
Set up private CA server
Necessary skills for mobile terminal test: ADB command and packet capturing
Project management tool - Introduction and practice of Alibaba cloud projex
Unity LOD
I have seven schemes to realize web real-time message push, seven!
How to publish your own NPM package
2022-07-24: what is the output of the following go language code? A:[]int{}; B:[]int(nil); C:panic; D: Compilation error. package main import ( “fmt“ ) f
rhcsa暑假第三天