当前位置:网站首页>[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;
}
边栏推荐
- Go Foundation (I)
- [new function] ambire wallet integrates Metis network
- The people's Bank of China printed and distributed the notice on supporting cross-border RMB settlement of new foreign trade formats
- Has my future been considered in the cloud native development route?
- Memo pattern
- 什么是匿名内部类,如何使用匿名内部类
- BERT和ViT简介
- 【HackTheBox】dancing(SMB)
- Cisco voice card handling configuration
- Introduction to Bert and Vit
猜你喜欢

iNFTnews | 元宇宙技术将带来全新的购物体验

Mécanisme d'attention du canal de fixation

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

直播预约|AWS Data Everywhere 系列活动

How to test electronic components with a multimeter

Flyweight pattern

SEAttention 通道注意力機制

Decorator Pattern

How to solve startup failure due to insufficient MySQL memory

LabVIEW显示Unicode字符
随机推荐
Has my future been considered in the cloud native development route?
docker 创建的 mysql8 怎么改密码
Mediator pattern
Nuxt - set SEO related tags, page titles, icons, etc. separately for each page (page configuration head)
Actual combat! Another opening method of magic modified swagger and knife4j
Sword finger offer II 040 Largest rectangle in matrix
Is the interviewer too difficult to serve? A try catch asks so many tricks
快速开发项目-VScode插件
JDBC man Han building code
1018 锤子剪刀布
How to create robots Txt file?
The virtual machine MySQL cannot be connected to the local computer
Inftnews | metauniverse technology will bring a new shopping experience
C language -- branch structure
Quelles sont les méthodes de simulation et de gravure des programmes? (comprend les outils communs et la façon dont ils sont utilisés)
Hot renewal process
系统分析师备考经验分享:分阶段、分重点
Project development culture
JDBC learning
Redis cache penetration, cache breakdown, cache avalanche