当前位置:网站首页>Leetcode15 -- sum of three numbers
Leetcode15 -- sum of three numbers
2022-07-27 22:19:00 【Peihj2021】
leetcode15– Sum of three numbers
Topic link :leetcode15
Title Description

title
Title details address :bilibili
Code
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;
}
}
Reference resources
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
边栏推荐
- 时间继电器
- leetcode15--三数之和
- ThreadLocal principle and source code analysis (click in step by step, don't recite, learn ideas)
- ApacheSpark-命令执行(CVE-2022-33891) 漏洞复现
- 学完4种 Redis 集群方案要多久?我一口气给你说完
- Reentranlock and source code analysis (learn ideas and click the source code step by step)
- Import word document pictures blocking and non blocking IO operations
- [question 22] dungeons and Warriors (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)
- Enumeration and annotation
- How to learn object Defineproperty | an article takes you to quickly learn
猜你喜欢

Starrocks community structure comes out, waiting for you to upgrade!

Regular expression exercise

Interview question: what are the functions of fail safe mechanism and fail fast mechanism

关系型数据库的设计思想,20张图给你看的明明白白
![[Marine Science] climate indices data set](/img/0f/f63b946b80fc2cf1d39a975c318abc.png)
[Marine Science] climate indices data set

Apachespark command execution (cve-2022-33891) vulnerability recurrence
![[question 21] idiom Solitaire (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)](/img/dd/5ef46cff3988db57bfaf6fe925a0e0.jpg)
[question 21] idiom Solitaire (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)

Optocoupler relay

Are Transformers Effective for Time Series Forecasting?|填坑

Nine days later, we are together to focus on the new development of audio and video and mystery technology
随机推荐
Temperature relay
Apachespark command execution (cve-2022-33891) vulnerability recurrence
How to buy stocks on mobile phones? Is it safe to open an account
STM32 project Sharing -- mqtt intelligent access control system (including app control)
ApacheSpark-命令执行(CVE-2022-33891) 漏洞复现
【海洋科学】海洋气候指数【Climate Indices】数据集
九天后我们一起,聚焦音视频、探秘技术新发展
Log4j vulnerability is still widespread and continues to cause impact
Monitor the running of server jar and restart script
STM32项目分享---MQTT智能门禁系统(含APP控制)
Excel only wants to visualize charts and make data move? Yes, come and watch (with a large number of templates to download)
@Can component be used on the same class as @bean?
Go language learning notes - mutex start go language from scratch
8000 word explanation of OBSA principle and application practice
MySQL execution process and order
[stonedb fault diagnosis] MDL lock waiting
【StoneDB故障诊断】系统资源瓶颈诊断
8000 word explanation of OBSA principle and application practice
EC code introduction
8000字讲透OBSA原理与应用实践