当前位置:网站首页>leetcode15--三数之和
leetcode15--三数之和
2022-07-27 19:26:00 【Peihj2021】
leetcode15–三数之和
题目链接:leetcode15
题目描述

题目解析
题目详细说明地址:bilibili
代码
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> arrayList = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] == nums[i-1]){
continue;
}
int left = i+1;
int right = nums.length - 1;
while (left < right){
int sum = nums[i] + nums[left] + nums[right];
if (sum > 0){
right--;
}else if (sum < 0){
left ++;
}else {
arrayList.add(Arrays.asList(nums[i],nums[left],nums[right]));
while (left < right && nums[right] == nums[right-1]){
right--;
}
while (left < right && nums[left] == nums[left+1]){
left++;
}
right--;
left++;
}
}
}
return arrayList;
}
}
参考
https://leetcode.cn/problems/3sum/
https://programmercarl.com/0015.%E4%B8%89%E6%95%B0%E4%B9%8B%E5%92%8C.html#%E5%93%88%E5%B8%8C%E8%A7%A3%E6%B3%95
边栏推荐
- Leetcode-152- product maximum subarray
- 九天后我们一起,聚焦音视频、探秘技术新发展
- Common shortcut keys and setting methods of idea
- An article takes you into the world of pycharm - stop asking me about pycharm installation and environment configuration!!!
- matlab 绘制三坐标(轴)图
- [stonedb fault diagnosis] MDL lock waiting
- How to learn object Defineproperty | an article takes you to quickly learn
- 【StoneDB故障诊断】MDL锁等待
- Log4j 漏洞仍普遍存在?
- Matplotlib 多子图绘制
猜你喜欢

项目分析(哪些是it培训给不了)

How can anyone ask how MySQL archives data?

Project analysis (what is it training that can't be given)

阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍

Deepfake's face is hard to distinguish between true and false, and musk Fenke has disguised successfully

Is log4j vulnerability still widespread?

8000字讲透OBSA原理与应用实践

固体继电器

Station B collapsed. What did the developer responsible for the repair do that night?

An2021软件安装及基本操作(新建文件/导出)
随机推荐
JVM garbage collection garbage collector and common combination parameters
Enumeration and annotation
8000字讲透OBSA原理与应用实践
Leetcode-155-minimum stack
MySQL execution process and order
极化继电器
JVM memory model interview summary
Excalidraw: an easy-to-use online, free "hand drawn" virtual whiteboard tool
8000字讲透OBSA原理与应用实践
[Marine Science] climate indices data set
项目分析(从技术到项目、产品)
Leetcode-55-jump game
Lvs+kept highly available cluster
Mimx8md6cvahzab i.MX 8mdual cortex-a53 - Microprocessor
Open source data quality solution -- Apache Griffin primer
2021-11-05 understanding of class variables and class methods
How to buy stocks on mobile phones? Is it safe to open an account
[C language] high precision addition, subtraction, multiplication and division template
Read Plato farm's eplato and the reason for its high premium
An2021软件安装及基本操作(新建文件/导出)