当前位置:网站首页>【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;
}
}
边栏推荐
- Use c language to implement tic-tac-toe chess (with source code, you can run it directly)
- stack stack
- 字节(byte)和位(bit)
- Getting Started with JNI
- 【LeetCode-205】同构字符串
- Here is a memorial
- C语言-7月21日-指针的深入
- Manufacturer Push Platform-Huawei Access
- js learning advanced BOM part (pink teacher notes)
- Vscode remote connection server terminal zsh+Oh-my-zsh + Powerlevel10 + Autosuggestions + Autojump + Syntax-highlighting
猜你喜欢

Jetpack使用异常问题集锦

Day 80

微信小程序启动页的实现

将一个excel文件中多个sheet页“拆分“成多个“独立“excel文件

Jetpack's dataBinding

Interpretation of the paper: GAN and detection network multi-task/SOD-MTGAN: Small Object Detection via Multi-Task Generative Adversarial Network

He Kaiming's new work ViTDET: target detection field, subverting the concept of layered backbone

The Summer of Open Source 2022 is coming | Welcome to sign up for the OpenMLDB community project~

星盟-pwn-babyheap

buuctf hacknote
随机推荐
Day 71
Certificate of SearchGuard configuration
编译异常解决
js 学习进阶(事件高级 pink老师教学笔记)
第六届蓝帽杯 EscapeShellcode
2022DASCTF X SU 三月春季挑战赛 checkin ROPgadget进阶使用
Day 69
Minutes of OpenMLDB Meetup No.2
The official website of OpenMLDB is upgraded, and the mysterious contributor map will take you to advance quickly
本地服务配置内网穿透实现微信公众号整合
Day 84
C语言实现扫雷游戏
swagger常用注释API @ApiModel、@ApiModelProperty的用法
Day 83
C语言-7月18日-二维数组的学习
微信小程序云开发项目wx-store代码详解
Goldbach's conjecture and the ring of integers
Day 80
杀死进程-查看防火墙状态
vim 编辑器使用学习