当前位置:网站首页>Force deduction brush questions, sum of three numbers
Force deduction brush questions, sum of three numbers
2022-07-26 09:26:00 【Xiao Tang Xuejie】
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 :[]
source : Power button (LeetCode)
link :https://leetcode.cn/problems/3sum
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
(1) First, the array is null Or less than 3 Array of , Directly back to the set .
(2) Sort the array ,
(3) Traversing sorted arrays , Define left and right pointers , Left pointer L by i+1, Right pointer R Is array length l-1.
(4) Ergodic condition ,L<R, Defining variables sum, if sum[i] + sum[L] + sum[R] = 0 Continue to cycle .
(5) if sum<0, The left pointer moves back ,sum > 0 The right pointer moves forward
(6) Be careful , if L and R The next element of is equal , To get rid of duplicate !
(7) call sort() Sort 、aList() Replace the array with a set .
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
int l = nums.length;
// Special judgement
if(nums == null && l< 3) return list;
// Sort
Arrays.sort(nums);
for(int i = 0; i < l ;i++){
if(i > 0 && nums[i] == nums[i - 1 ]) continue;
int L = i + 1;// Left pointer
int R = l - 1;// Right pointer
while( L < R){
int sum = nums[i] + nums[L] + nums[R];
if(sum == 0){
//Arrays.asList() Convert an array to a collection
list.add(Arrays.asList(nums[i],nums[L],nums[R]));
while(L < R && nums[L] == nums[L + 1]) L ++;// If the value of the next element of the left pointer is equal to the current value , There will be repeated results , Duplicate values need to be removed
while(L < R && nums[R] == nums[R - 1]) R--;
// If the value of the next element of the right pointer is equal to the current value , There will be repeated results , Duplicate values need to be removed
L++;
R--;
}
else if(sum < 0) L++;
else if(sum > 0) R--;
}
}
return list;
}
}边栏推荐
猜你喜欢

Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing

Paper notes: knowledge map kgat (unfinished temporary storage)

Redis principle and use - Basic Features

(2006, MySQL server has gone away) problem handling

语音聊天app源码——钠斯直播系统源码

Vertical search

mysql5.7.25主从复制(单向)

【线上问题】Timeout waiting for connection from pool 问题排查

cocoapods的安装和使用

附加到进程之后,断点显示“当前不会命中断点 还没有为该文档加载任何符号”
随机推荐
Android implements the caching mechanism and caches multiple data types
2022 mobile crane driver test question simulation test question bank simulation test platform operation
安卓 实现缓存机制,多种数据类型缓存
神经网络与深度学习-6- 支持向量机1 -PyTorch
Implementation of fragment lazy loading after multi-layer nesting
Windows backs up the database locally by command
dll中的全局变量
760. String length
Jmeter配置元件之CSV数据文件设置
会议OA项目(三)---我的会议(会议排座、送审)
Vertical search
What are CSDN spaces represented by
Windows通过命令备份数据库到本地
[online problem] timeout waiting for connection from pool problem troubleshooting
VS2019配置opencv
Apple generated and verified tokens for PHP
Ext4 file system opens dir_ After nlink feature, link_ Use link after count exceeds 65000_ Count=1 means the quantity is unknown
微信小程序开发
2022 chemical automation control instrument operation certificate test question simulation test platform operation
phpexcel导出emoji符号报错