当前位置:网站首页>【Hot100】15. 三数之和
【Hot100】15. 三数之和
2022-06-30 06:47:00 【王六六的IT日常】
15. 三数之和
中等题
排序+ 双指针
参考题解: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;
}
}
边栏推荐
- Definition and use of ROS topic messages
- KEIL - 下载调试出现“TRACE HW not present”
- High performance distributed execution framework ray
- Fastapi learning Day2
- Porting RT thread to s5p4418 (V): thread communication
- Force buckle ------ replace blank space
- ini解析學習文檔
- Keil - the "trace HW not present" appears during download debugging
- List in set (2)
- Notes: environment variables
猜你喜欢
随机推荐
CPU到底是怎么识别代码的?
High performance distributed execution framework ray
关注这场直播,了解能源行业双碳目标实现路径
Imxq Freescale yocto project compilation record
原来你是这样的数组,终于学会了
Unable to read file for extraction: gdx64. dll
Idea add database
基础刷题(一)
Rising posture series: fancy debugging information
1.3 - 码制
Vscode returns the previous cursor (previous browse position)
RT thread Kernel Implementation (I): threads and scheduling
2020-10-06
RT thread Kernel Implementation (II): critical area, object container
1.8 - 多级存储
Gazebo model modification
ftplib+ tqdm 上传下载进度条
【Mask-RCNN】基于Mask-RCNN的目标检测和识别
RT thread migration to s5p4418 (I): scheduler
1.8 - multi level storage









