题目
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
示例:
给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]
链接:https://leetcode-cn.com/problems/3sum
分析及反思
先是特判断少于三个元素(包括空) 直接返回null答案
然后会数组排序
锁定第一个元素 然后分别从最左和最右边开始包夹 逼近0来求ans,并对相同的元素去重
当 i的数值大于0右边的也肯定大于零了

代码
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ans = new ArrayList();//创建答案数组列表
if (nums == null || nums.length < 3) return ans;// 特判断 空 或 少于三个元素
Arrays.sort(nums);//排序
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) break;// 如果当前数字大于0,则三数之和一定大于0,所以结束循环
if (i > 0 && nums[i] == nums[i - 1]) continue;// i 去重
int l = i + 1, r = nums.length - 1;//i l r 三个指针 i固定 移动 lr
while (l < r) {
int sum = nums[i] + nums[l] + nums[r];
if (sum > 0) r--;//sum太大了 最右边的最大的 缩小
else if (sum < 0) l++;//太小了 最左边的最小增大
else {//sum==0
ans.add(Arrays.asList(nums[i], nums[l], nums[r]));//加入的答案中
while (l < r && nums[l] == nums[l + 1]) l++;// l去重
while (l < r && nums[r] == nums[r - 1]) r--;// r 去重
//移动到不重复的位置
l++;
r--;
}
}
}
return ans;
}
}
本文作者:Author: 寒光博客
文章标题:[LeetCode]15. 三数之和
本文地址:https://dxoca.cn/Algorithm/365.html 百度已收录
版权说明:若无注明,本文皆为“Dxoca's blog (寒光博客)”原创,转载请保留文章出处。
本文地址:https://dxoca.cn/Algorithm/365.html 百度已收录
版权说明:若无注明,本文皆为“Dxoca's blog (寒光博客)”原创,转载请保留文章出处。
好的算法解析 真的是一种享受,,可惜我的能力还达不到.(╯‵□′)╯︵┴─┴