当前位置:网站首页>[code random entry - hash table] T15, sum of three numbers - double pointer + sort
[code random entry - hash table] T15, sum of three numbers - double pointer + sort
2022-06-29 04:34:00 【Not blogging】
T15、 Sum of three numbers
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 .
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 .
The hardest part of this question is how to deal with repeated results ;
So it's sort + The idea of double pointer
Recycle in the outer layer [0,n) determine a = nums[i], And then in i+1,n Use double pointers between
public List<List<Integer>> threeSum(int[] nums) {
int n = nums.length;
List<List<Integer>> ret = new ArrayList<>();
if(n < 3){
return ret;
}
Arrays.sort(nums);
// int i = 0, l = i+1, h = n-1;
// a b c
for(int i = 0; i < n; i++){
if(nums[i] > 0){
return ret;
}
if(i > 0 && nums[i] == nums[i-1]){
continue;
}
int l = i+1, h = n-1;
while(l < h){
int a = nums[i];
int b = nums[l];
int c = nums[h];
if(a + b + c == 0){
ret.add(Arrays.asList(a,b,c));
while(l < h && nums[h] == nums[h-1]){
h--;
}
while(l < h && nums[l] == nums[l+1]){
l++;
}
h--;
l++;
}else if(a + b + c < 0){
l++;
}else{
h--;
}
}
}
return ret;
}
边栏推荐
- Implementation of thread pool based on variable parameter template
- 1017 a divided by B
- Cucumber test practice
- Error accessing database
- If I hadn't talked to Ali P7, I wouldn't know I was a mallet
- How to use the select statement of MySQL
- How to display all MySQL databases
- I came from a major, so I didn't want to outsource
- The subnet of the pool cannot be overlapped with that of other pools.
- ROS URDF model is parsed into KDL tree
猜你喜欢

C language -- branch structure

1018 锤子剪刀布

Decorator Pattern

Log in to the MySQL database and view the version number on the command line

I came from a major, so I didn't want to outsource

安捷伦数字万用表软件NS-Multimeter,实时数据采集数据自动保存

Remediation for Unsafe Cryptographic Encryption

The 30th day of force deduction (DP topic)

Anaconda's own Spyder editor starts with an error

JVM_ 16_ Garbage collector
随机推荐
1015 theory of virtue and talent
JSX的基本使用
Go Foundation (I)
[new function] ambire wallet integrates Metis network
Here comes Wi Fi 7. How strong is it?
什么是匿名内部类,如何使用匿名内部类
【牛客网刷题系列 之 Verilog快速入门】~ 异步复位的串联T触发器
Log in to the MySQL database and view the version number on the command line
What is the method of connection query in MySQL
Inftnews | metauniverse technology will bring a new shopping experience
What are the basic usage methods of MySQL
LabVIEW displays Unicode characters
Analysis on the types of source code anti leakage technology
ROS URDF model is parsed into KDL tree
Seattention channel attention mechanism
1018 锤子剪刀布
1019 digital black hole
SEAttention 通道注意力機制
GBASE 8s must be a DBSA、路径更改导致无法启动的解决方法
ECS 4 sync point, write group, version number