当前位置:网站首页>【Hot100】15. Sum of three numbers
【Hot100】15. Sum of three numbers
2022-06-30 06:53:00 【Wang Liuliu's it daily】
15. Sum of three numbers
Medium question
Sort + Double pointer
Refer to the explanation of the question :https://leetcode.cn/problems/3sum/solution/3sumpai-xu-shuang-zhi-zhen-yi-dong-by-jyd/
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;
}
}
边栏推荐
猜你喜欢
原来你是这样的数组,终于学会了
Go installation and configuration (1)
Rising posture series: fancy debugging information
1.5 - 逻辑运算
Numpy中的四个小技巧
Introduction to programming ape (11) -- structure
1.8 - 多级存储
Never forget the original intention, and be lazy if you can: C # operate word files
Judge whether H5 is in wechat environment or enterprise wechat environment at both ends
Four tips in numpy
随机推荐
Graphic octet, really top
【每日一题】535. TinyURL 的加密与解密
Keil - the "trace HW not present" appears during download debugging
图解八股,真的太顶了
Getting started with research
[mask RCNN] target detection and recognition based on mask RCNN
Finished product upgrade procedure
Use of sscanf function
0基础转行软件测试,如何实现月薪9.5k+
第一行代码(第三版)学习笔记
InnoDB engine in MySQL
RT thread Kernel Implementation (I): threads and scheduling
我开户后把账号忘记了咋办?股票在网上开户安全吗?
1.7 - CPU performance indicators
成品升级程序
原理:WebMvcConfigurer 与 WebMvcConfigurationSupport避坑指南
Porting RT thread to s5p4418 (II): dynamic memory management
Porting RT thread to s5p4418 (V): thread communication
Fastapi learning Day2
tomorrow! "Mobile cloud Cup" competition air publicity will start!