当前位置:网站首页>【LeetCod】三数之和-15
【LeetCod】三数之和-15
2022-08-11 05:30:00 【Ring*】
6.11 三数之和【15】
6.11.1 题目描述
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组。
6.11.2 方法一:排序 + 双指针
下面给出了改进的方法的伪代码实现:
nums.sort()
for first = 0 .. n-1
// 只有和上一次枚举的元素不相同,我们才会进行枚举
if first == 0 or nums[first] != nums[first-1] then
for second = first+1 .. n-1
if second == first+1 or nums[second] != nums[second-1] then
for third = second+1 .. n-1
if third == second+1 or nums[third] != nums[third-1] then
// 判断是否有 a+b+c==0
check(first, second, third)
nums.sort()
for first = 0 .. n-1
if first == 0 or nums[first] != nums[first-1] then
// 第三重循环对应的指针
third = n-1
for second = first+1 .. n-1
if second == first+1 or nums[second] != nums[second-1] then
// 向左移动指针,直到 a+b+c 不大于 0
while nums[first]+nums[second]+nums[third] > 0
third = third-1
// 判断是否有 a+b+c==0
check(first, second, third)
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int n = nums.length;
Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<List<Integer>>();
// 枚举 a
for (int first = 0; first < n; ++first) {
// 需要和上一次枚举的数不相同
if (first > 0 && nums[first] == nums[first - 1]) {
continue;
}
// c 对应的指针初始指向数组的最右端
int third = n - 1;
int target = -nums[first];
// 枚举 b
for (int second = first + 1; second < n; ++second) {
// 需要和上一次枚举的数不相同
if (second > first + 1 && nums[second] == nums[second - 1]) {
continue;
}
// 需要保证 b 的指针在 c 的指针的左侧
while (second < third && nums[second] + nums[third] > target) {
--third;
}
// 如果指针重合,随着 b 后续的增加
// 就不会有满足 a+b+c=0 并且 b<c 的 c 了,可以退出循环
if (second == third) {
break;
}
if (nums[second] + nums[third] == target) {
List<Integer> list = new ArrayList<Integer>();
list.add(nums[first]);
list.add(nums[second]);
list.add(nums[third]);
ans.add(list);
}
}
}
return ans;
}
}
复杂度分析
- 时间复杂度: O ( N 2 ) O(N^2) O(N2),其中 N 是数组 nums 的长度。
- 空间复杂度:O(logN)。我们忽略存储答案的空间,额外的排序的空间复杂度为 O(logN)。然而我们修改了输入的数组nums,在实际情况下不一定允许,因此也可以看成使用了一个额外的数组存储了 nums 的副本并进行排序,空间复杂度为 O(N)。
6.11.3 my answer—排序 + 双指针
class Solution {
public static List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
Arrays.sort(nums); // 排序之后,选择的三个元素 a<b<c 可有效去重
int n = nums.length;
if(n<3)return null;
for (int i = 0; i < n-2; i++) {
if(i>0 && nums[i] == nums[i-1])continue; // 第一个元素a去重
int left = i+1;
int right = n-1;
int target = -nums[i];
while(left<right){
if(nums[left]+nums[right]==target){
if(nums[left]==nums[left+1] && right-left != 1){
// 第二个元素b去重
left++;
continue;
}
if(nums[right]==nums[right-1] && right-left != 1){
// 第三个元素c去重
right--;
continue;
}
List<Integer> list1 = new ArrayList<>();
list1.add(nums[i]);
list1.add(nums[right]);
list1.add(nums[left]);
list.add(list1);
right--;
left++;
}else if(nums[left]+nums[right] > target){
right--;
}else {
left++;
}
}
}
return list;
}
}
边栏推荐
- Building a data ecology for feature engineering - Embrace the open source ecology, OpenMLDB fully opens up the MLOps ecological tool chain
- js 学习进阶(事件高级 pink老师教学笔记)
- Day 73
- C语言-6月10日-my_strcat函数的编写
- 开发公众号授权遇到的redirect_uri参数错误
- 使用adb命令管理应用
- He Kaiming's new work ViTDET: target detection field, subverting the concept of layered backbone
- Day 83
- 手把手导入企业项目(快速完成本地项目配置)
- C语言-7月31日-指针的总结以及typedef关键字
猜你喜欢
随机推荐
星盟-pwn-babyheap
JNI入门
Day 79
ARM assembly instruction ADR and LDR
基于微信小程序云开发实现的电商项目,可以自行定制开发
【无标题】
精彩联动 | OpenMLDB Pulsar Connector原理和实操
swagger常用注释API @ApiModel、@ApiModelProperty的用法
C语言-6月8日-求两个数的最小公倍数和最大公因数;判断一个数是否为完数,且打印出它的因子
The official website of OpenMLDB is upgraded, and the mysterious contributor map will take you to advance quickly
使用adb命令管理应用
Goldbach's conjecture and the ring of integers
C语言-6月10日-my_strcpy函数的编写
Jetpack使用异常问题集锦
Byte (byte) and bit (bit)
Tinker's self-introduction
JS小技巧,让你编码效率杠杠的,快乐摸鱼
C语言实现扫雷游戏
He Kaiming's new work ViTDET: target detection field, subverting the concept of layered backbone
星盟-pwn-babyfmt