当前位置:网站首页>力扣刷题,三数之和
力扣刷题,三数之和
2022-07-26 08:53:00 【小唐学姐】
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组。
示例 1:
输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]
示例 2:
输入:nums = []
输出:[]
示例 3:
输入:nums = [0]
输出:[]
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/3sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路:
(1)首先对数组为null或者小于3的数组,直接返回集合。
(2)对数组进行排序,
(3)遍历排序后的数组,定义左指针和右指针,左指针L为i+1,右指针R为数组长度l-1.
(4)遍历条件,L<R,定义变量sum,若sum[i] + sum[L] + sum[R] = 0继续循环。
(5)若sum<0,左指针后移,sum > 0右指针前移
(6)注意,若L和R的下一个元素相等,要去掉重复的!
(7)调用sort()排序、aList()将数组换成集合。
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
int l = nums.length;
//特判
if(nums == null && l< 3) return list;
//排序
Arrays.sort(nums);
for(int i = 0; i < l ;i++){
if(i > 0 && nums[i] == nums[i - 1 ]) continue;
int L = i + 1;//左指针
int R = l - 1;//右指针
while( L < R){
int sum = nums[i] + nums[L] + nums[R];
if(sum == 0){
//Arrays.asList()将数组转换成集合
list.add(Arrays.asList(nums[i],nums[L],nums[R]));
while(L < R && nums[L] == nums[L + 1]) L ++;//如果左指针的下一个元素的值等于当前的值,就会出现重复的结果,需要把重复的值去掉
while(L < R && nums[R] == nums[R - 1]) R--;
//如果右指针的下一个元素的值等于当前的值,就会出现重复的结果,需要把重复的值去掉
L++;
R--;
}
else if(sum < 0) L++;
else if(sum > 0) R--;
}
}
return list;
}
}边栏推荐
- After MySQL 8 OCP (1z0-908), hand in your homework
- [search topics] flood coverage of search questions after reading the inevitable meeting
- Espressif 玩转 编译环境
- Foundry tutorial: writing scalable smart contracts in various ways (Part 1)
- at、crontab
- Human computer interaction software based on C language
- PAT 甲级 A1076 Forwards on Weibo
- js闭包:函数和其词法环境的绑定
- zsh: command not found: nvm
- One click deployment of lamp and LNMP scripts is worth having
猜你喜欢
![[freeswitch development practice] user defined module creation and use](/img/5f/3034577e3e2bc018d0f272359af502.png)
[freeswitch development practice] user defined module creation and use

at、crontab

My meeting of OA project (query)

Huffman transformation software based on C language
![[untitled]](/img/89/c3ab79eb325f0136114a568745924b.png)
[untitled]

CSDN Top1 "how does a Virgo procedural ape" become a blogger with millions of fans through writing?

Poor English, Oracle OCP or MySQL OCP exam can also get a high score of 80 points

机器学习中的概率模型

CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主?

JDBC database connection pool (Druid Technology)
随机推荐
day06 作业--增删改查
Day 6 summary & database operation
Which financial product has the highest yield in 2022?
正则表达式:判断是否符合USD格式
Kotlin属性与字段
SSH,NFS,FTP
网络安全漫山遍野的高大上名词之后的攻防策略本质
The lessons of 2000. Web3 = the third industrial revolution?
Hegong sky team vision training Day6 - traditional vision, image processing
Learning notes of automatic control principle - Performance Analysis of continuous time system
Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
Oracle 19C OCP 1z0-082 certification examination question bank (51-60)
[recommended collection] MySQL 30000 word essence summary index (II) [easy to understand]
数据库操作技能7
Database operation topic 1
tcp 解决short write问题
[suggestions collection] summary of MySQL 30000 word essence - locking mechanism and performance tuning (IV) [suggestions collection]
In the first year of L2, the upgrade of arbitrum nitro brought a more compatible and efficient development experience
Implementation of Prometheus web authentication and alarm
187. Repeated DNA sequence