当前位置:网站首页>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);
}边栏推荐
- Restore the default routing settings of the primary network card
- Leetcode topic analysis spiral matrix II
- Point cloud library PCL from introduction to mastery Chapter 10
- When easynvr service is started, video cannot be played due to anti-virus software interception. How to deal with it?
- 点云库pcl从入门到精通 第十章
- 3. caller service call - dapr
- Hongmeng reads the resource file
- Tencent cloud arm server evaluation practice
- Unique paths for leetcode topic resolution
- Open source stealing malware mercurial found in the field for "educational purposes"
猜你喜欢

Testing -- automated testing selenium (about API)

【云计算】GFS思想优势以及架构

6月《中国数据库行业分析报告》发布!智能风起,列存更生

Talk about the implementation principle of @autowired

Open source technology exchange batch stream integrated data synchronization engine Chunjun data restore DDL function module analysis

点云库pcl从入门到精通 第十章

最常用的5中流ETL模式

986. Interval List Intersections

为什么用生长型神经气体网络(GNG)?
![[cloud computing] GFS ideological advantages and architecture](/img/98/2a4ef0ca805add24d431dac9808903.png)
[cloud computing] GFS ideological advantages and architecture
随机推荐
The results of CDN node and source station are inconsistent
[QNX Hypervisor 2.2用户手册]6.2 网络
[qnx hypervisor 2.2 user manual]5.6.1 silent device during guest shutdown
How to use the template library of barcode label software
[paper notes] catching both gray and black swans: open set supervised analog detection*
[advanced Android] kotlin notes
Paper reading [quovadis, action recognition? A new model and the dynamics dataset]
The most commonly used 5-stream ETL mode
Kernel log debugging method
Single core driver module
Leetcode topic analysis spiral matrix II
3、 System analysis and design
2-用线段构成图形、坐标转换
Can portals be the next decentraland?
Assembly (receive several n-digit decimal values (0~65535) from the keyboard and display their sum in different base numbers.)
测试-- 自动化测试selenium(关于API)
438. Find All Anagrams in a String
Introduction to typescript and basic types of variable definitions
Data assets are king, analyzing the relationship between enterprise digital transformation and data asset management
USB peripheral driver - configfs