当前位置:网站首页>【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;
}
}
边栏推荐
猜你喜欢

Bat 使用细节2

Keil - the "trace HW not present" appears during download debugging

Never forget the original intention, and be lazy if you can: C # operate word files

CPU到底是怎么识别代码的?

判断h5在两端是在微信环境还是企业微信环境

Analysis of startup process of gazebo multi computer simulation

原理:WebMvcConfigurer 与 WebMvcConfigurationSupport避坑指南

1.7 - CPU的性能指标

Initial love with mqtt

It turns out that you are such an array. You have finally learned
随机推荐
1.4 - fixed and floating point numbers
1.8 - 多级存储
RT thread migration to s5p4418 (I): scheduler
[fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network
ftplib+ tqdm 上传下载进度条
【每日一题】535. TinyURL 的加密与解密
【我的创作纪念日】一周年随笔
Control method of UAV formation
Force buckle ------ replace blank space
Relevant database questions.
NFS mount
Introduction to programming ape (11) -- structure
银河麒麟初体验
RT thread migration to s5p4418 (IV): thread synchronization
Connect to remote server
Steps for formulating class or file templates in idea
leetcode:98. Validate binary search tree
File Transfer Protocol,FTP文件共享服务器
HuaWei满级大牛首次分享出这份598页网络协议全彩手册
原来你是这样的数组,终于学会了