当前位置:网站首页>【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 usage details 2
- RT thread migration to s5p4418 (IV): thread synchronization
- 【Hot100】15. 三数之和
- Imxq Freescale yocto project compilation record
- Never forget the original intention, and be lazy if you can: C # operate word files
- Performance comparison of random network, scale-free network, small world network and NS small world matlab simulation
- 0 basic job transfer software test, how to achieve a monthly salary of 9.5k+
- [transfer] analysis of memory structure, cache and DMA architecture
- Authority management system
- Basic questions (I)
猜你喜欢

Rising posture series: fancy debugging information

基础刷题(一)

InnoDB engine in MySQL

HuaWei满级大牛首次分享出这份598页网络协议全彩手册

Introduction to programming ape (11) -- structure

RT thread migration to s5p4418 (IV): thread synchronization

关注这场直播,了解能源行业双碳目标实现路径

SOC_SD_CLK

Why does ETL often become ELT or even let?

File transfer protocol, FTP file sharing server
随机推荐
阿里云买的40G高效云盘挂载只有20G
深度学习---三好学生各成绩所占权重问题(3)
Rising posture series: fancy debugging information
memcpy内存重叠的解决
C语言:练习题三
1.6 - CPU composition
No module named 'pyqt5 QtMultimedia‘
RT thread application
InnoDB engine in MySQL
Ffmplay is not generated during the compilation and installation of ffmpeg source code
[fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network
Several C language implementations
High performance distributed execution framework ray
Bat 使用细节2
安装setup对应的组件
SOC_SD_CLK
Practice summary of Prometheus project in amu Laboratory
Never forget the original intention, and be lazy if you can: C # operate word files
0基础转行软件测试,如何实现月薪9.5k+
1.9 - Classification of memory