当前位置:网站首页>[record of question brushing] 15 Sum of three numbers
[record of question brushing] 15 Sum of three numbers
2022-07-20 21:40:00 【InfoQ】
One 、 Title Description
Input :nums = [-1,0,1,2,-1,-4]
Output :[[-1,-1,2],[-1,0,1]]
Input :nums = []
Output :[]
Input :nums = [0]
Output :[]
- 0 <= nums.length <= 3000
- -105 <= nums[i] <= 105
Two 、 Thought analysis
After ordering , Double pointer solution
- We define a pointer num[i] Traverse the sorted array
- In order to make the sum of the three numbers 0 Then there must be both negative and positive numbers , therefore num[i] In certain cases , Let's define two more pointers (L,R) Traverse from both sides , send
L + num[i] + R == 0
That is, a set of triples , During traversal :
- If and
Greater than 0
, explainR
Too big ,R
Move left
- If and
Less than 0
, explain L
Too small ,L
Move right
- Duplicate element occurred skip
- dissatisfaction L < R end
nums[i] > 0
Because it's sorted , So there can't be three numbers that add up to 0
, Go straight back to
- The array is
null
Or the array length is less than 3
, Go straight back to
3、 ... and 、 Code implementation
class Solution {
public static List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList();
int len = nums.length;
if(nums == null || len < 3) return res;
// Sort
Arrays.sort(nums);
for (int i = 0; i < len ; i++) {
// If the current number is greater than 0, Then the sum of three numbers must be greater than 0, So end the cycle
if(nums[i] > 0) break;
// duplicate removal
if(i > 0 && nums[i] == nums[i-1]) continue;
int L = i + 1;
int R = len - 1;
while(L < R){
int sum = nums[i] + nums[L] + nums[R];
if(sum == 0){
res.add(Arrays.asList(nums[i],nums[L],nums[R]));
// duplicate removal
while (L<R && nums[L] == nums[L+1]) L++;
while (L<R && nums[R] == nums[R-1]) R--;
L++;
R--;
}
else if (sum < 0) L++;
else if (sum > 0) R--;
}
}
return res;
}
}
Complexity analysis
n
nums
Running results

summary
边栏推荐
- Skywalking全链路监控集群和动态部署
- 数据从Oracle迁移至PolarDB for PostgreSQL的过程中,数据迁移具体指什么?
- Unity3D学习笔记9——加载纹理
- 牛客每日刷题之数组
- Summary on sorting and de duplication of sets
- What is the function of dbc2000? Installation and configuration of dbc2000
- Redis is awesome. If you don't understand the usage specification, it will be ruined
- 如何在微信小游戏制作工具中实现递归函数
- Dest0g3 520迎新赛-web-funny_upload
- 記錄一下十三届藍橋杯嵌入式省賽題目
猜你喜欢
LeetCode 69:爬楼梯 / 跳台阶
数据治理研究报告——数据要素权益配置路径(2022年),50页pdf
排序--插入排序、希尔排序
KubeSphere 3.3.0 离线安装教程
Vmware解决无法识别USB的问题
Given the preorder traversal and the inorder traversal order of a binary tree, find the postorder traversal of the tree
2019电赛复测测试题
y71.第四章 Prometheus大厂监控体系及实战 -- prometheus server安装(二)
【golang从入门到实践】扑克发牌游戏
Part of the second Shanxi Network Security Skills Competition (Enterprise Group) WP (V)
随机推荐
Array of daily questions for Niuke
当元宇宙的发展开始越来越多地呈现互联网的样式时,我们需要警惕
【golang从入门到实践】扑克发牌游戏
Firewall related
你来追我呀!Flutter 实现追逐动画
2022.07.19 洛谷 P6588 『JROI-1』 向量
Come after me! Flutter realizes chasing animation
手撕快速排序
Skywalking full link monitoring cluster and dynamic deployment
2019电赛复测测试题
About the list loop (five ways of writing foreach)
Unity3d learning note 9 - loading textures
2022 latest Inner Mongolia construction safety officer simulation question bank and answers
【云图说】第248期 图解公网域名解析:轻松实现域名访问网站/邮箱
东莞证券买股票开户安全吗?
The R language uses the gghistogram function of ggpubr package to visualize the grouping box diagram, add the grouping mean value, customize the grouping color, add the axial whisker diagram (rug), ad
Given the preorder traversal and the inorder traversal order of a binary tree, find the postorder traversal of the tree
What is the difference between server bandwidth and home broadband?
How to quickly get started with find and xargs commands
Win10+libtorch+yolov5-6.0 deployment