当前位置:网站首页>力扣刷题,三数之和
力扣刷题,三数之和
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;
}
}
边栏推荐
- [recommended collection] summary of MySQL 30000 word essence - partitions, tables, databases and master-slave replication (V)
- unity简易消息机制
- Huffman transformation software based on C language
- tcp 解决short write问题
- 深度学习常用激活函数总结
- 公告 | FISCO BCOS v3.0-rc4发布,新增Max版,可支撑海量交易上链
- day06 作业---技能题7
- PAT 甲级 A1076 Forwards on Weibo
- Study notes of automatic control principle -- correction and synthesis of automatic control system
- Poor English, Oracle OCP or MySQL OCP exam can also get a high score of 80 points
猜你喜欢
Human computer interaction software based on C language
pl/sql之集合-2
机器学习中的概率模型
C Entry series (31) -- operator overloading
Regular expression: judge whether it conforms to USD format
公告 | FISCO BCOS v3.0-rc4发布,新增Max版,可支撑海量交易上链
深度学习常用激活函数总结
[eslint] Failed to load parser ‘@typescript-eslint/parser‘ declared in ‘package. json » eslint-confi
Set of pl/sql -2
JS file import of node
随机推荐
The lessons of 2000. Web3 = the third industrial revolution?
Set of pl/sql -2
数据库操作 题目一
CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主?
sklearn 机器学习基础(线性回归、欠拟合、过拟合、岭回归、模型加载保存)
pycharm 打开多个项目的两种小技巧
[freeswitch development practice] use SIP client Yate to connect freeswitch for VoIP calls
[recommended collection] MySQL 30000 word essence summary + 100 interview questions (I)
机器学习中的概率模型
Study notes of automatic control principle -- dynamic model of feedback control system
Solve the problem of C # calling form controls across threads
海内外媒体宣发自媒体发稿要严格把握内容关
220. Presence of repeating element III
uni-app 简易商城制作
Mutual transformation of array structure and tree structure
Cadence (x) wiring skills and precautions
C#入门系列(三十一) -- 运算符重载
Neo eco technology monthly | help developers play smart contracts
[search topics] flood coverage of search questions after reading the inevitable meeting
Typescript encryption tool passwordencoder