当前位置:网站首页>[leetcode] 15. Sum of three numbers
[leetcode] 15. Sum of three numbers
2022-06-30 12:13:00 【Xiaoqu】
15、 Sum of three numbers
subject :
Give you one containing n Array of integers nums, Judge nums Are there three elements in a,b,c , bring a + b + c = 0 ? Please find out all and for 0 Triple without repetition .
Be careful : Answer cannot contain duplicate triples .
Example 1:
Input :nums = [-1,0,1,2,-1,-4]
Output :[[-1,-1,2],[-1,0,1]]
Example 2:
Input :nums = []
Output :[]
Example 3:
Input :nums = [0]
Output :[]
Tips :
0 <= nums.length <= 3000
-10^5 <= nums[i] <= 10 ^5
Their thinking :
Sort + Double pointer
The difficulty of this problem is how to get rid of repeated solutions .
Algorithm flow :
- Special judgement , For array length n, If the array is null Or the array length is less than 3, return [ ].
- Sort the array .
- Traversing the sorted array :
- if nums[i]>0nums[i]>0: Because it's sorted , So there can't be three numbers that add up to 00, Direct return .
- For repeating elements : skip , Avoid duplicate solutions
- Left pointer L=i+1, Right pointer R=n-1, When L<R when , Execute loop :
- When nums[i]+nums[L]+nums[R]==0, Execute loop , Judge whether the left and right bounds are repeated with the next position , Remove duplicate solutions . At the same time L,R Move to the next position , Looking for new solutions
- If sum is greater than 0, explain nums[R] Too big ,R Move left
- If sum is less than 0, explain nums[L] Too small ,L Move right
Reference code :
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> resList = new ArrayList<>();
// Special condition judgment
if (nums.length < 3) return resList;
else if (nums.length == 3){
if (nums[0] + nums[1] + nums[2] == 0) {
List<Integer> resSingle = new ArrayList<>();
resSingle.add(nums[0]);
resSingle.add(nums[1]);
resSingle.add(nums[2]);
resList.add(resSingle);
}
return resList;
} else {
// 1. Sort
Arrays.sort(nums);
// 2. Double pointer
for (int j = 0; j < nums.length - 2; j++) {
if (j > 0) {
if (nums[j - 1] == nums[j]) continue;
}
int target = - nums[j];
int l = j + 1;
int r = nums.length - 1;
while (l < r) {
int sum = nums[l] + nums[r];
if (sum < target) l++;
else if (sum > target) r--;
else {
List<Integer> resSingle = new ArrayList<>();
resSingle.add(nums[j]);
resSingle.add(nums[l]);
resSingle.add(nums[r]);
resList.add(resSingle);
l++;
r--;
while (nums[l] == nums[l-1] && nums[r] == nums[r+1] && l < r) {
l++;
r--;
}
}
}
}
return resList;
}
}
}

边栏推荐
猜你喜欢
随机推荐
Yolov5 export the pit encountered by onnx
Set集合
会议预告 | 华为 2012 实验室全球软件技术峰会-欧洲分会场
拆分电商系统为微服务
beego开发博客系统学习(二)
21、wpf之绑定使用小记
Typescript readonlyarray (read only array type) details
ClipboardJS——开发学习总结1
【LeetCode】15、三数之和
DMA controller 8237a
Talk about how to do hardware compatibility testing and quickly migrate to openeuler?
When building the second website with pagoda, the website always reports an error: no input file specified
The website with id 0 that was requested wasn‘t found. Verify the website and try again
AutoCAD - len command
R language ggplot2 visualization: use ggplot2 visualization scatter diagram and the color parameter in AES function to specify that data points in different groups are displayed in different colors
wallys/IPQ8074a/2x(4 × 4 or 8 × 8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD
Swagger2自动生成APi文档
Subtrate 源码追新导读-5月上旬: XCM 正式启用
NoSQL——Redis的配置与优化
Cache avalanche and cache penetration solutions









