当前位置:网站首页>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
边栏推荐
- After sorting (bubble sorting), learn to continuously update other sorting methods
- 2021-11-05 understanding of class variables and class methods
- Memo mode - unity
- Leetcode-226-flip binary tree
- 温度继电器
- An2021 software installation and basic operation (new file / export)
- Inventory Poka ecological potential project | cross chain characteristics to promote the prosperity of multi track
- Pythia: Facebook's latest open source visual and language multitasking learning framework
- Understanding of L1 regularization and L2 regularization [easy to understand]
- 【OBS】P B 丢帧阈值 buffer_duration_usec
猜你喜欢

Matlab draws the statistical rose chart of wind speed and direction

项目分析(从技术到项目、产品)
Excalidraw:很好用的在线、免费「手绘」虚拟白板工具

Apachespark command execution (cve-2022-33891) vulnerability recurrence

Talk about MySQL transaction two-phase commit

@Component可以和@Bean 用在同一个类上吗?

Huaneng Fujian company and Huawei digital energy deepen strategic cooperation and jointly build a low-carbon smart county

Inertial navigation principle (VII) -imu error classification (II) -allan variance analysis method +imu test + calibration introduction

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

Inventory Poka ecological potential project | cross chain characteristics to promote the prosperity of multi track
随机推荐
高频继电器
Reentranlock and source code analysis (learn ideas and click the source code step by step)
Tencent cloud [hiflow] | automation --------- hiflow: still copying and pasting?
一种比读写锁更快的锁,还不赶紧认识一下
C language output teaching calendar
[question 21] idiom Solitaire (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)
V2.X 同步异常,无法云端同步的帖子一大堆,同步又卡又慢
The source code of live broadcast app system, and the rotation diagram of upper and lower layers
Leetcode-226-flip binary tree
Nano semiconductor 65W gallium nitride (GAN) scheme was adopted by Xiaomi 10 Pro charger
视频直播源码,uni-app实现广告滚动条
Mimx8md6cvahzab i.MX 8mdual cortex-a53 - Microprocessor
8000字讲透OBSA原理与应用实践
Station B collapsed. If we were the developer responsible for the repair that night
First knowledge of esp8266 (I) -- access point and wireless terminal mode
How to customize logging of.Net6.0
Monitor the running of server jar and restart script
Cocoapods reload
项目分析(哪些是it培训给不了)
An2021软件安装及基本操作(新建文件/导出)