当前位置:网站首页>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;
}
}
边栏推荐
- Preliminary understanding of Panda3D particle system
- 05. Libavformat Library of ffmpeg
- Add click event to unity 3D object
- [c language] custom type (structure ~ enumeration ~ Union)
- [globally unique ID] how to handle the ID primary key after dividing the database and table?
- Learning records [email protected] R & D effectiveness measurement indicators
- Shenzhen on call test, subject 4 on call test, subject 3 theory, second theory on call test instructions
- "Niuke | daily question" inverse Polish expression
- How to publish your own NPM package
- 教你三招从让性能从20s优化到500ms
猜你喜欢

自己实现is_class

epoll的实现原理
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

教你三招从让性能从20s优化到500ms
![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](/img/bf/e38a8fd813f88a83f61a1abfa3b95d.png)
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
![2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f](/img/bf/e38a8fd813f88a83f61a1abfa3b95d.png)
2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f

Shenzhen on call test, subject 4 on call test, subject 3 theory, second theory on call test instructions
搭建私有CA服务器

HMS Core Discovery第16期直播预告|与虎墩一起,玩转AI新“声”态

"Niuke | daily question" inverse Polish expression
随机推荐
DOM在Ahooks中的处理过程
Add transition layer to JS page
harbor安装
Json.tojsonstring cannot pass Boolean
Win11 how to view the file explorer tab
[the most comprehensive and detailed] how to design a database and table splitting scheme that can dynamically expand and shrink capacity?
STM32 Development Notes 119: what macros are required to enable FPU?
接口幂等性
Why does the official not recommend using UUID as MySQL primary key
The 6th "Blue Hat Cup" National College Students' Cyber Security Skills Competition writeup
[c language] custom type (structure ~ enumeration ~ Union)
Delivery practice of private PAAS platform based on cloud native
服务器防护的七个建议
微信小程序相关操作示例
JS common code questions array de duplication - Custom New throttling and anti shake - deep copy - instanceof URL parameter extraction - thousand separator - array to tree structure - array flattening
Purpose of setting novice task period in the integral system
STM32 development note 120: solve the problem that%f in printf cannot be output
Performance Optimization: lazy loading of pictures
Wechat official account all article download links to get
Logu p3398 hamsters find sugar solution