当前位置:网站首页>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
边栏推荐
- 一篇文章看懂变量提升(hoisting)
- 求3个字符串(每串不超过20个字符)中的最大者。
- SAP self created table log function is enabled
- 使用 Ingress 实现金丝雀发布
- Three minutes to teach you to use Houdini fluid > > to solve particle fluid droplets
- Analysis of Nacos source code
- Anti shake and throttling
- MySQL -- the secret of lock -- how to lock data
- Move layout (Flex layout, viewport label)
- 学fpga(从verilog到hls)
猜你喜欢

雲計算技能大賽 -- openstack私有雲環境 第一部分

国基北盛-openstack-容器云-环境搭建

Cloud computing skills competition -- Part 2 of openstack private cloud environment

MySQL -- the secret of lock -- how to lock data

Concours de compétences en informatique en nuage - - première partie de l'environnement cloud privé openstack

Intranet penetration series: ICMP of Intranet tunnel_ Tran

AAAI 2022招募讲者啦!!

输入 “ net start mysql ”,出现 “ 发生系统错误 5。 拒绝访问 ” 。问题详解

【无标题】

upload-labs 靶场练习
随机推荐
Buuctf misc brush questions
数据库之Mysql——概述安装篇
每周leetcode - 06 数组专题 7~739~50~offer 62~26~189~9
php高精度计算
BUUCTF [ACTF2020 新生赛]Include1
[go] common concurrency model [generic version]
Interview learning route
Research on software security based on NLP (I)
简述CPU
1+x云计算中级--脚本搭建读写分离
yum源仓库本地搭建的两种方法
Depth of binary tree
SAP self created table log function is enabled
Thinkphp6 + JWT realizes login verification
Smart business card applet business card details page function implementation key code
CTF attack and defense world brush questions 51-
How does feign integrate hystrix
strcat()、strcpy()、strcmp()、strlen()
sentinel集成nacos动态更新数据原理
Introduction to sap query enhanced development