当前位置:网站首页>Leetcode question brushing Diary - 15 Sum of three
Leetcode question brushing Diary - 15 Sum of three
2022-04-22 20:50:00 【Light ink @ ~ no trace】
Give you one containing n Array of integers nums, Judge nums Are there three elements in a,b,c , bring a + b + c = 0 ? Please find out all and for 0 Triple without repetition .
Be careful :
Answer cannot contain duplicate triples .
Example 1:
Input :nums = [-1,0,1,2,-1,-4]
Output :[[-1,-1,2],[-1,0,1]]
Example 2:
Input :nums = []
Output :[]
Example 3:
Input :nums = [0]
Output :[]
Tips :
0 <= nums.length <= 3000
-105 <= nums[i] <= 105
Their thinking :
First step : First, judge whether the length of the passed in array is greater than or equal to 3, Or judge whether the array is empty , If the array length is less than 3 Or the array is empty , Then directly return an empty list .
The second step : Sort the array , Can be called directly Arrays Sorting provided by tool class API Interface sort() Method .
The third step : Go through every number , Then take three of them and add them , Judge whether the sum is zero , Zero use HashSet For storage (HashSet Class cannot store duplicate numbers , This can solve the problem of de duplication )
Step four : take HashSet convert to List, And return data .
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> lists = new ArrayList<>();
if(nums.length < 3 || nums == null){
return lists;
}
Arrays.sort(nums);
HashSet<List<Integer>> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
if(nums[i] > 0){
break;
}
int L = i+1, R = nums.length - 1;
while (L < R){
int sum = nums[i] + nums[L] + nums[R];
if (sum == 0){
set.add(Arrays.asList(nums[i], nums[L++], nums[R--]));
}else if(sum < 0){
L ++;
}else if(sum > 0){
R --;
}
}
}
for (List<Integer> list: set) {
lists.add(list);
}
return lists;
}
}
Their thinking 2:
The basic idea is consistent with the above , Just don't use HashSet Deduplication , Instead, use array subscripts for de duplication .
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int n = nums.length;
Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<List<Integer>>();
// enumeration a
for (int first = 0; first < n; ++first) {
// It needs to be different from the last enumeration
if (first > 0 && nums[first] == nums[first - 1]) {
continue;
}
// c The corresponding pointer initially points to the rightmost end of the array
int third = n - 1;
int target = -nums[first];
// enumeration b
for (int second = first + 1; second < n; ++second) {
// It needs to be different from the last enumeration
if (second > first + 1 && nums[second] == nums[second - 1]) {
continue;
}
// Need assurance b The pointer is in c On the left side of the pointer
while (second < third && nums[second] + nums[third] > target) {
--third;
}
// If the pointers coincide , With b Subsequent additions
// There will be no satisfaction a+b+c=0 also b<c Of c 了 , You can exit the loop
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;
}
}
Their thinking 3:
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for(int k = 0; k < nums.length - 2; k++){
if(nums[k] > 0) break;
if(k > 0 && nums[k] == nums[k - 1]) continue;
int i = k + 1, j = nums.length - 1;
while(i < j){
int sum = nums[k] + nums[i] + nums[j];
if(sum < 0){
while(i < j && nums[i] == nums[++i]);
} else if (sum > 0) {
while(i < j && nums[j] == nums[--j]);
} else {
res.add(new ArrayList<Integer>(Arrays.asList(nums[k], nums[i], nums[j])));
while(i < j && nums[i] == nums[++i]);
while(i < j && nums[j] == nums[--j]);
}
}
}
return res;
}
}
版权声明
本文为[Light ink @ ~ no trace]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222050016156.html
边栏推荐
- Who is important about products and services? Changan Ford tells you "all"
- Improving fee shot part segmentation using course supervision
- 对Swin-T中SW-MSA的一些理解
- List的使用
- On "waiting for awakening mechanism"
- Redis's key and value best practices
- Leetcode学习计划之动态规划入门day1,2(共4题)
- Can you really use MySQL explain?
- 编程实用工具,总有一个你用的上
- 华为机考题汇总
猜你喜欢

Gtid replication of MySQL master-slave replication

L1-046 整除光棍

浅学 “等待唤醒机制 ”

Dialogue: Shen Si L, founder of papaya mobile, from Silicon Valley to Beijing

How to make robots more like "people" and make slams more flexible?

LeeCode 130. 被围绕的区域
After five years of Android, I successfully joined Tencent with this 190 page interview information

CmsEasy7.6.3.2逻辑漏洞

Matlab learning notes - calculate eigenvectors and manually execute PCA

Leetcode Hot 100
随机推荐
The interviewer would rather have my younger brother who has just graduated and worked for one year than me who has worked for five years, with an annual salary of 25W
Virtual machine building and installation pulsar environment tutorial (for development and testing)
Error running ‘JeecgSystemApplication‘: Command line is too long. Shorten command line for JeecgSyst
中美程序员对比:你认同吗
Huawei computer test question - hj61 put apple
Gtid replication of MySQL master-slave replication
Active mode and passive mode of FTP
Character array and string: delete all spaces in the string. (10 points) write a function to delete all spaces in the string.
MySQL 第8章 数据库约束
用MarkDown写PPT
MATLAB学习笔记 - 计算特征向量手动执行PCA
Use constant member functions for constant types (design a date class and time)
MySQL的explain,你真的会用吗?
华为机考题汇总
[radar] point target simulation of simulated synthetic aperture radar (SAR)
The traffic not included in the Internet era is all included in the industrial Internet era
[200 opencv routines of youcans] 160 Otsu method of image processing
The annual salary is 170W. Alibaba P8 blind date requires the woman's monthly salary of 10000. Netizen: it's a little high
H. Maximal AND
虚拟机搭建安装Pulsar环境教程(开发测试使用)