当前位置:网站首页>Three different numbers with 0 in leetcode/ array
Three different numbers with 0 in leetcode/ array
2022-07-28 08:33:00 【xcrj】
Code
package com.xcrj;
import java.util.*;
/** * The finger of the sword Offer II 007. Array and 0 Three numbers of * Arrays are unordered * Given an inclusion n Array of integers nums, Judge nums Are there three elements in a ,b ,c , bring a + b + c = 0 ? Please find all and for 0 Triple without repetition * a + b + c = 0 And ask for a b c Are different */
public class Solution7 {
/** * Convert to sword finger Offer II 006. Sort the sum of two numbers in the array problem * Sort the array * a + b = c */
public List<List<Integer>> threeSum1(int[] nums) {
// result ,
Set<List<Integer>> set = new HashSet<>(3);
// Ascending sort above set?, The title requires that the results are not repeated , for example -4,-1,-1,0,1,2.[-1,0,1],[-1,0,-1] It's repetitive
Arrays.sort(nums);
// Double pointer
for (int k = 0; k < nums.length; k++) {
// target=-c
int target = 0 - nums[k];
// from k The next element of starts applying double pointers
int i = k + 1;
int j = nums.length - 1;
while (i < j) {
int sum = nums[i] + nums[j];
if (sum == target) {
// asList(T... a) Pass in variable parameters , Variable length parameter
// !! add If the value is the same, discard (e==null ? e2==null : e.equals(e2))
// !! ArrayList Of equal() according to Object references are equal || The element values in the array are equal
set.add(Arrays.asList(nums[k], nums[i], nums[j]));
// There are new a+b+c=0 The situation of
i++;
j--;
} else if (sum < target) i++;
else j--;
}
}
// ArrayList(Collection<? extends E> c)
return new ArrayList<>(set);
}
/** * Convert to sword finger Offer II 006. Sort the sum of two numbers in the array problem * Sort the array * a + b = c */
public List<List<Integer>> threeSum2(int[] nums) {
// result ,
List<List<Integer>> lists = new ArrayList<>(3);
// Ascending sort above set?, The title requires that the results are not repeated , for example -4,-1,-1,0,1,2.[-1,0,1],[-1,0,-1] It's repetitive
Arrays.sort(nums);
// Double pointer
for (int k = 0; k < nums.length; k++) {
// duplicate removal k,k+1 and k The values of the elements are the same , Handle 1 Just one , To deal with the first k Just one
if (k > 0 && nums[k] == nums[k - 1]) continue;
// target=-c
int target = 0 - nums[k];
// from k The next element of starts applying double pointers
int i = k + 1;
int j = nums.length - 1;
while (i < j) {
int sum = nums[i] + nums[j];
if (sum == target) {
// asList(T... a) Pass in variable parameters , Variable length parameter
lists.add(Arrays.asList(nums[k], nums[i], nums[j]));
// duplicate removal , requirement a+b+c=0, a b c Are different
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}));
}
}
Reference resources
author :tangweiqun
link :https://leetcode.cn/problems/1fGaJU/solution/jian-dan-yi-dong-javac-pythonjs-san-shu-nu6el/
source : Power button (LeetCode)
边栏推荐
- MCU IO port controls 12V voltage on and off, MOS and triode circuit
- 记录一次mycat连接Communications link failure问题解决
- 网口网络水晶头RJ45、POE接口定义线序
- leetcode/单词长度的最大乘积
- 2022牛客多校第二场解题报告
- [leetcode] 24. Exchange nodes in the linked list in pairs
- 单片机IO口控制12V电压通断,MOS和三极管电路
- tkMapper的使用-超详细
- Es6: arrow function usage
- sql server时间字段排序
猜你喜欢

EMC EMI磁珠的特性

Record a MYCAT connection and solve the problems of communications link failure

Allure use

CarSim simulation quick start (10) - Modeling of braking system

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

一键开关机电路
![[Qt5] QT small software release](/img/83/9867bd4513caadac6a056c801abe48.png)
[Qt5] QT small software release

Draw.io image saving path settings

Change the dataDir path after mysql8.0.16 installation

Es6: template string
随机推荐
Prescan quick start to master the transportation elements in lesson 14, prescan
49-OpenCv深入分析轮廓
一键开关机电路
Fxksmdb.exe process description
(Reprinted) plantuml Quick Guide
PMP practice once a day | don't get lost in the exam -7.13
One key switch circuit
Enum class
Regular expression for mobile number verification
【MindSpore易点通机器人-01】你也许见过很多知识问答机器人,但这个有点不一样
CarSim simulation quick start (XI) - Driver Model (1)
JS cartoon English alphabet typing game source code
Usage of constructors
二维数组及操作
The fourth phase (2021-2022) research on the implementation of cloud native technology in traditional industries - central state-owned enterprises was officially released
Detailed explanation of random number generated by random class
Usage of qmap
Is the salary of test / development programmers unbalanced? Busy life, all kinds of job hopping
【OpenCV】生成透明的PNG图像
EMC EMI磁珠的特性