当前位置:网站首页>Leetcode topic analysis 3sum
Leetcode topic analysis 3sum
2022-06-23 08:48:00 【ruochen】
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
- Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
- The solution set must not contain duplicate triplets.``` For example, given array S = {-1 0 1 2 -1 -4}, A solution set is: (-1, 0, 1) (-1, -1, 2) ``` analysis :
Sort the array ;
start from 0 To n-1, Yes numstart Find the other two numbers , Here we use mid and end;
mid Point to start+1,q Point to the end .sum = numstart + nummid+ numend;
Use the forcing theorem to solve , The termination condition is mid == end;
Incidental weight removal
duplicate removal :
If start here we are start+1,numstart == numstart - 1, Then the solution must be repeated ;
If mid++,nummid = nummid - 1, Then the solution must be repeated .
public List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length < 3) {
return new ArrayList<List<Integer>>();
}
Set<List<Integer>> set = new HashSet<List<Integer>>();
Arrays.sort(nums);
for (int start = 0; start < nums.length; start++) {
if (start != 0 && nums[start - 1] == nums[start]) {
continue;
}
int mid = start + 1, end = nums.length - 1;
while (mid < end) {
int sum = nums[start] + nums[mid] + nums[end];
if (sum == 0) {
List<Integer> tmp = new ArrayList<Integer>();
tmp.add(nums[start]);
tmp.add(nums[mid]);
tmp.add(nums[end]);
set.add(tmp);
while (++mid < end && nums[mid - 1] == nums[mid])
;
while (--end > mid && nums[end + 1] == nums[end])
;
}
else if (sum < 0) {
mid++;
}
else {
end--;
}
}
}
return new ArrayList<List<Integer>>(set);
}边栏推荐
- Fraction to recursing decimal
- Subsets of leetcode topic resolution
- 自组织映射神经网络(SOM)
- Install a WGet for your win10
- 词性家族
- [qnx hypervisor 2.2 user manual]6.1 using the QNX hypervisor system
- How to restore visualizations and dashboards after kibana rebuilds the index
- Kernel log debugging method
- 【学习资源】理解数学和热爱数学
- 297. Serialize and Deserialize Binary Tree
猜你喜欢

通信方式总结及I2C驱动详解

Multi-scale feature combination in target detection

自组织映射神经网络(SOM)

Data assets are king, analyzing the relationship between enterprise digital transformation and data asset management

Introduction to typescript and basic types of variable definitions

GeoServer adding mongodb data source

173. Binary Search Tree Iterator

渲染效果图哪家好?2022最新实测(四)
![Paper reading [quovadis, action recognition? A new model and the dynamics dataset]](/img/3f/449cc91bfa66fcf26bc2cd405fb773.png)
Paper reading [quovadis, action recognition? A new model and the dynamics dataset]

'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册
随机推荐
Go data types (II) overview of data types supported by go and Boolean types
On the light application platform finclip and the mobile application development platform mpaas
驱动架构 & platform平台总线驱动模型
Summary ranges of leetcode topic resolution
In June, China database industry analysis report was released! Smart wind, train storage and regeneration
You have a string of code, but do not support the lower version of go; Judge the go version number, you deserve it!
Balls and cows of leetcode topic analysis
Map interface and its sub implementation classes
Set interface and set sub implementation classes
[qnx hypervisor 2.2 user manual]6.2 network
List interface three sub implementation classes
训练后的随机森林模型导出和加载
高通9x07两种启动模式
The first day of employment more than ten years ago
Why is the easycvr Video Fusion platform offline when cascading with the Hikvision platform? How to solve it?
单编内核驱动模块
Data assets are king, analyzing the relationship between enterprise digital transformation and data asset management
6、 Web Architecture Design
How to sort a dictionary by value or key?
TDesign update weekly report (the first week of January 2022)