当前位置:网站首页>LeetCode15. Sum of three
LeetCode15. Sum of three
2022-04-23 08:09:00 【Want to get into Ali's chicken】
Ideas
Use double pointers to .
First, sort the array .
Three pointers , A pointer to the current element i, A left pointer left, A right pointer right.
Give Way i Traverse the array subscript at once ,left=i+1;right = nums.length-1;temp = nums[i]+nums[left]+nums[right]; When temp Greater than 0 when , will right--; When temp Less than 0 Just when left++; When temp==0 Just when left++,right--;
The traversal result does not exist set in ,set Can automatically remove duplicate elements . The final will be set To list that will do .
Code
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
int left = 0;
int right = nums.length-1;
List<List<Integer>> res = new ArrayList<>();
Set<List<Integer>> t = new HashSet<>();
int temp = 0;
for(int i = 0;i<=right;i++){
left = i+1;
right = nums.length-1;
while(right>left){
temp = nums[i]+nums[left]+nums[right];
if( temp== 0){
t.add(Arrays.asList(nums[i], nums[left], nums[right]));
right--;
left++;
}else if(temp>0){
right--;
}else{
left++;
}
}
}
for(List l :t){
res.add(l);
}
return res;
}
}
版权声明
本文为[Want to get into Ali's chicken]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230641067687.html
边栏推荐
- Intranet penetration series: ICMP of Intranet tunnel_ Tran
- upload-labs 靶场练习
- The displayed amount of ABAP ALV is inconsistent with the exported amount
- 一篇文章看懂变量提升(hoisting)
- 云计算赛项--2020年赛题基础部分[任务3]
- Intranet penetration series: icmptunnel of Intranet tunnel (Master James Barlow's)
- 数据库之MySQL——基本常用查询命令
- Draw a circle quickly in MATLAB (the one that can be drawn directly given the coordinates and radius of the center of the circle)
- Go语学习笔记 - Slice、Map | 从零开始Go语言
- 访问数据库的时候出现错误 Operation not allowed for a result set of type ResultSet.TYPE_FORWARD_ONLY.详解
猜你喜欢

Redis -- why is the string length of string emstr the upper limit of 44 bytes?

Research on software security based on NLP (2)

Chapter V investment real estate
![[极客大挑战 2019]Havefun1](/img/8b/b15bf31771d54db25f24d630e64093.png)
[极客大挑战 2019]Havefun1

yum源仓库本地搭建的两种方法

SAP self created table log function is enabled

sentinel集成nacos动态更新数据原理

数据安全问题已成隐患,看vivo如何让“用户数据”重新披甲

Buctf MISC brossage

雲計算技能大賽 -- openstack私有雲環境 第一部分
随机推荐
upload-labs 靶场练习
雲計算技能大賽 -- openstack私有雲環境 第一部分
智能名片小程序名片详情页功能实现关键代码
Go语学习笔记 - Slice、Map | 从零开始Go语言
DVWA靶场练习
LeetCode 1611. 使整数变为 0 的最少操作次数
C 输出一种二维数组,特点如下。
如何在SQL Server中导入excel数据,2019版
数据库之Mysql——概述安装篇
Flutter之Provider共享数据的两种方式
1216_MISRA_C规范学习笔记_控制流的规则要求
Feign source code analysis
Principle of sentinel integrating Nacos to update data dynamically
Three minutes to teach you to use Houdini fluid > > to solve particle fluid droplets
Research on system and software security (4)
[programming practice / embedded competition] learning record of embedded competition (I): establishment of TCP server and web interface
Intranet penetration series: dnscat2 of Intranet tunnel
Go语学习笔记 - 结构体 | 从零开始Go语言
数据库之MySQL——基本常用查询命令
CTF attack and defense world brush questions 51-