当前位置:网站首页>【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;
}
}
边栏推荐
- How does the CPU recognize the code?
- HuaWei满级大牛首次分享出这份598页网络协议全彩手册
- ROS-URDF
- 基础刷题(一)
- RT thread Kernel Implementation (III): implementation of idle threads and blocking delay
- Install the components corresponding to setup
- Pay attention to this live broadcast and learn about the path to achieve the dual carbon goal of the energy industry
- 【json-tutorial】第一章学习笔记
- Cmake post makefile:32: * * * missing separator Stop.
- 写一个C程序判断系统是大端字节序还是小端字节序
猜你喜欢

RT thread Kernel Implementation (I): threads and scheduling

Initial love with mqtt

Px4 control mode summary

1.9 - 存储器的分类

File transfer protocol, FTP file sharing server

How to set the hot deployment of idea web project

图解八股,真的太顶了

Basic questions (I)

Xshell传输文件

Keil - the "trace HW not present" appears during download debugging
随机推荐
Force buckle ------ replace blank space
1.6 - CPU composition
Px4 control mode summary
力扣------替换空格
RT thread migration to s5p4418 (III): static memory pool management
Four tips in numpy
Install the components corresponding to setup
The solution of memcpy memory overlap
Problems and solutions of creating topic messages in ROS
C language: exercise 3
Notes: environment variables
相关数据库问题提问。
0 basic job transfer software test, how to achieve a monthly salary of 9.5k+
NFS mount
记录一次腾讯测试开发工程师自动化接口测试实践经验
SOC_AHB_SD_IF
Write a C program to judge whether the system is large end byte order or small end byte order
What if I forget my account number after opening an account? Is it safe to open an account online?
File Transfer Protocol,FTP文件共享服务器
图解八股,真的太顶了