当前位置:网站首页>leetcode/数组中和为0的三个不同数
leetcode/数组中和为0的三个不同数
2022-07-28 06:33:00 【xcrj】
代码
package com.xcrj;
import java.util.*;
/** * 剑指 Offer II 007. 数组中和为 0 的三个数 * 数组是无序的 * 给定一个包含 n 个整数的数组nums,判断nums中是否存在三个元素a ,b ,c ,使得a + b + c = 0 ?请找出所有和为 0 且不重复的三元组 * a + b + c = 0并且要求a b c都不相同 */
public class Solution7 {
/** * 转换为剑指 Offer II 006. 排序数组中两个数字之和 问题 * 对数组排序 * a + b = c */
public List<List<Integer>> threeSum1(int[] nums) {
// 结果,
Set<List<Integer>> set = new HashSet<>(3);
// 升序排序 上面set?,题目要求结果不重复,例如 -4,-1,-1,0,1,2。[-1,0,1],[-1,0,-1]就是重复的
Arrays.sort(nums);
// 双指针
for (int k = 0; k < nums.length; k++) {
// target=-c
int target = 0 - nums[k];
// 从k的下一个元素开始应用双指针
int i = k + 1;
int j = nums.length - 1;
while (i < j) {
int sum = nums[i] + nums[j];
if (sum == target) {
// asList(T... a) 传入可变参数,变长参数
// !! add值相同则丢弃 (e==null ? e2==null : e.equals(e2))
// !! ArrayList的equal() 根据 对象引用相等||数组中元素值相等
set.add(Arrays.asList(nums[k], nums[i], nums[j]));
// 存在新的a+b+c=0的情况
i++;
j--;
} else if (sum < target) i++;
else j--;
}
}
// ArrayList(Collection<? extends E> c)
return new ArrayList<>(set);
}
/** * 转换为剑指 Offer II 006. 排序数组中两个数字之和 问题 * 对数组排序 * a + b = c */
public List<List<Integer>> threeSum2(int[] nums) {
// 结果,
List<List<Integer>> lists = new ArrayList<>(3);
// 升序排序 上面set?,题目要求结果不重复,例如 -4,-1,-1,0,1,2。[-1,0,1],[-1,0,-1]就是重复的
Arrays.sort(nums);
// 双指针
for (int k = 0; k < nums.length; k++) {
// 去重k,k+1和k个元素值相同,处理1个即可,处理第k个即可
if (k > 0 && nums[k] == nums[k - 1]) continue;
// target=-c
int target = 0 - nums[k];
// 从k的下一个元素开始应用双指针
int i = k + 1;
int j = nums.length - 1;
while (i < j) {
int sum = nums[i] + nums[j];
if (sum == target) {
// asList(T... a) 传入可变参数,变长参数
lists.add(Arrays.asList(nums[k], nums[i], nums[j]));
// 去重,要求a+b+c=0, a b c都不相同
while (i < j && nums[i] == nums[++i]) ;
while (i < j && nums[j] == nums[--j]) ;
} else if (sum < target) i++;
else j--;
}
}
return lists;
}
public static void main(String[] args) {
Solution7 solution7 = new Solution7();
System.out.println(solution7.threeSum2(new int[]{
-1, 0, 1, 2, -1, -4}));
}
}
参考
作者:tangweiqun
链接:https://leetcode.cn/problems/1fGaJU/solution/jian-dan-yi-dong-javac-pythonjs-san-shu-nu6el/
来源:力扣(LeetCode)
边栏推荐
- Chapter 01 introduction of [notes of Huashu]
- Enum class
- Usage of qgroupbox
- Window 2 - > toolbar (28-1)
- Talk about row storage and column storage of database
- Mechanical revolution Jiaolong P wired network card driver can't play
- The core packages and middleware required for golang development cover all areas of the project and are worth collecting
- Prescan quick start to master the track editing path of Lecture 16
- 【活动报名】云原生技术交流 Meetup,8 月 6 日广州见
- QT uses semaphores to control threads (qsemaphore)
猜你喜欢

PMP practice once a day | don't get lost in the exam -7.13

Basic dictionary of deep learning --- activation function, batch size, normalization

记录一次mycat连接Communications link failure问题解决

单片机IO口控制12V电压通断,MOS和三极管电路
![[Qt5] small software with 5 people randomly selected from the bid evaluation expert base](/img/ca/9f6bd6af45e2113c050edf3a65aaf2.png)
[Qt5] small software with 5 people randomly selected from the bid evaluation expert base

【花书笔记】 之 Chapter01 引言

百度智能云九州区县大脑,描绘城乡新蓝图!

These mobile security browsers are more than a little easy to use

Characteristics of EMC EMI beads

jquey的基础语法
随机推荐
uniapp的swiper动态设置current值不生效解决办法
Chapter 01 introduction of [notes of Huashu]
百度智能云九州区县大脑,描绘城乡新蓝图!
Information system project manager must recite the core examination site (41) risk management plan
JS thoroughly understand this point
Es6: template string
Yaml parameter configuration based on singleton mode
Parse tree structure JS
Spiral matrix
Recommend a fully open source, feature rich, beautiful interface mall system
Viewing vantage's self drive from the "three good" kitchen electricity standard and the value proposition of "serious life"
js卡片层叠样式的图片切换js特效
QT uses semaphores to control threads (qsemaphore)
Mysql, how many columns can be used to create an index?
[Qt5] QT small software release
记录一次mycat连接Communications link failure问题解决
单片机IO口控制12V电压通断,MOS和三极管电路
Pytorch的冻结以及解冻
Prescan quick start to master the transportation elements in lesson 14, prescan
pyspark 写入数据到iceberg