当前位置:网站首页>Leetcode 15: sum of three numbers
Leetcode 15: sum of three numbers
2022-07-25 05:19:00 【Swarford】
subject :
Ideas : Sort + Double pointer
nSum The core idea of a series of questions is to sort + Double pointer ;
Why can't the sum of two numbers use double pointers ?
Double pointers require array sorting , The sum of two numbers needs to return the index , After sorting, the index is messy !
Two pointer thought :
according to sum Resize the left and right pointers ;
however , This implementation will result in duplicate results , for instance nums = [1,1,1,2,2,3,3], target = 4, In the result [1,3] It will be repeated ;
So when nums[left] With the next number nums[left+1] Equal is equal left++ skip ,right Empathy !
Be careful :
Sum of three ,left Must be from i+1 Start , Otherwise, the double pointer will be used repeatedly num[i] !
Java Realization :
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
// The number returned is not the index !
List<List<Integer>> r=new ArrayList<>();
if(nums.length==0){
return r;
}
// 1. Prioritize
Arrays.sort(nums);
for(int i=0;i<nums.length;i++){
// duplicate removal 1
if(i>0 && nums[i]==nums[i-1]){
continue; // Traverse to the next i
}
// If i>0, And the array is increasing , Is doomed to be unable to come up with 0 !
if(nums[i]>0){
continue;
}
// 2. Double pointer found 0-nums[i] Two numbers of ,
int target=0-nums[i];
int left=i+1; // left Must be from i+1 Start , Otherwise, it will be reused num[i] !
int right=nums.length-1;
while(left<right){
int sum=nums[left]+nums[right];
// according to sum To adjust the pointer
if(sum>target){
right--;
}else if(sum<target){
left++;
}else if(sum==target){
r.add(Arrays.asList(nums[i],nums[left],nums[right]));
// duplicate removal 2: Added to... For the first time List Then we have to go to the heavy !
while(left<right && nums[left]==nums[left+1]){
left++;
}
while(left<right && nums[right]==nums[right-1]){
right--;
}
// Keep looking for
right--;
left++;
}
}
}
return r;
}
}
边栏推荐
- 使用getifaddrs获取本机网口IP地址
- 微信小程序相关操作示例
- Document collaboration tool recommendation
- The second day of rhcsa summer vacation
- What is virtual DOM? How to implement a virtual DOM
- [small program practice] first day
- Deep error
- How to carry out the function test with simple appearance and complex interior?
- 一篇文章带你读懂Redis的哨兵模式
- When image component in wechat applet is used as background picture
猜你喜欢

Pikachu vulnerability platform exercise

Solve the problem that uni app applet obtains routes and route parameters
[email protected] R & D effectiveness measurement indicators"/>Learning records [email protected] R & D effectiveness measurement indicators

Ping command

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

Document collaboration tool recommendation

Build keyword driven automated testing framework

一篇文章带你读懂Redis的哨兵模式

1310_一个printf的实现分析
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]
随机推荐
Shenzhen on call test, subject 4 on call test, subject 3 theory, second theory on call test instructions
STL notes (III): input / output stream
rhcsa暑假第三天
rhce第一天
Small case of data analysis: visualize recruitment data and view the most needed technologies in the field~
STM32 Development Notes 119: what macros are required to enable FPU?
一篇文章带你读懂Redis的哨兵模式
RHCE first day
搭建私有CA服务器
Forwarding and sharing function of wechat applet
STM32 development note 120: solve the problem that%f in printf cannot be output
rhcsa暑假第二天
项目管理工具——阿里云Projex介绍与实战
nacos中哪边有这个列的sql脚本啊?
I will write some Q & A blogs recently, mainly focusing on the points that are easy to have doubts.
微信小程序相关操作示例
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
Solve the problem that uni app applet obtains routes and route parameters
Redis cluster setup (Windows)
JWT(json web token)