LeetCode15 3Sum
题意:
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: The solution set must not contain duplicate triplets. (Medium)
For example, given array S = [-1, 0, 1, 2, -1, -4], A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
分析:
暴力搜索是O(n^3),利用2sum思路,三个元素相加为零,即两个元素相加等于另一个元素的相反数。
所以先排序,然后确定搜索顺序,对每一个元素,从其下一位和最后一个元素开始向中间搜索(类似two sum), 时间复杂度O(n^2)
注意问题:
1. 元素个数小于三个时候,直接返回空vector;
2. 遇到相同的选定时候需要跳过;
3. 遇到相同的待加数跳过(例如 0,-1,-1,1中,不跳过会出现两个0,-1,1在结果中)
4. 对于已经找到一组解之后,自己考虑的方案是,只把start++, 所以对于第3点的判定是判定nums[start]与nums[start -1]是否相同即可;
参考讨论区中的方案是,找到一组解之后,start++,end--只到与前一位不重复,可以作为参考。
代码1:
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> v;
if (nums.size() < ) {
return v;
}
for (int i = ; i < nums.size() - ; ++i) {
if (i >= && nums[i] == nums[i - ]) { //遇到相同的选定跳过
continue;
}
int start = i + , end = nums.size() - ;
while (start < end) {
if ( nums[i] + nums[start] + nums[end] == ) {
if (start > i + && nums[start] == nums[start - ]) { //[0,0,0,0]情况, 相同的相加元素
start++;
continue;
}
vector<int> temp{nums[i], nums[start], nums[end]};
v.push_back(temp);
start++;
}
else if (nums[i] + nums[start] + nums[end] > ) {
end--;
}
else {
start++;
}
}
}
return v;
}
};
代码2:
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> v;
if (nums.size() < ) { //数组元素个数过少,直接返回
return v;
}
for (int i = ; i < nums.size() - ; ++i) {
if (i >= && nums[i] == nums[i - ]) {
continue;
}
int start = i + , end = nums.size() - ;
while (start < end) {
if ( nums[i] + nums[start] + nums[end] == ) {
vector<int> temp{nums[i], nums[start], nums[end]};
v.push_back(temp);
start++;
end--;
while ( (start < end) && nums[start] == nums[start - ]) { //没加start < end虽然过了,估计是样例不够完善
start++;
}
while ( (start < end) && nums[end] == nums[end + ]) {
end--;
}
}
else if (nums[i] + nums[start] + nums[end] > ) {
end--;
}
else {
start++;
}
}
}
return v;
}
};
LeetCode15 3Sum的更多相关文章
- leetcode15—3Sum
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- LeetCode15——3Sum
数组中找三个数和为0的结果集 1 // 解法一:先排序 然后固定一个值 然后用求两个数的和的方式 public static List<List<Integer>> three ...
- Leetcode15.3Sum三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- LeetCode 15. 三数之和(3Sum)
15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...
- ARTS第六周
第六周.后期补完,太忙了. 1.Algorithm:每周至少做一个 leetcode 的算法题2.Review:阅读并点评至少一篇英文技术文章3.Tip:学习至少一个技术技巧4.Share:分享一篇有 ...
- [Swift]LeetCode15. 三数之和 | 3Sum
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- LeetCode: 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 3Sum algorithm - 非常容易理解的实现 (java)
原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
随机推荐
- HDU-3401 Trade 单调队列优化DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 DP方程容易想出来,f[i][j]表示第i天拥有j个股票的最优解,则: 1.不买不卖,f[i][ ...
- html5 拖拽
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- accordion data-options iconCls
<div id="aa" class="easyui-accordion" style="width:500px;height:300px;&q ...
- HDU 1847 Good Luck in CET-4 Everybody!(找规律,或者简单SG函数)
Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- Codeforces 597C. Subsequences (树状数组+dp)
题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长 ...
- 观察者模式:关于通知的使用(NSNotificationCenter)
一.通知的使用方法 1.发出通知 例如:[[NSNotificationCenter defaultCenter]postNotificationName:@"backToFirstPage ...
- spring与jpa整合 简化persistence.xml配置文件 使用属性文件 数据源dbcp访问数据库
===========appliction.xml配置文件======================= <?xml version="1.0" encoding=" ...
- Unity3d:加载Format是RGB24位的图片失败(加载图片显示问号)
问题描述:加载图片显示是个红色的问号,调试发现,Texture的Format=RGB24的都加载失败,ARGB32位的都能成功,按照常规,首先去度娘,看是否有人遇到和我同样的问题,结果一无所获.将用N ...
- CT值及CT常用窗宽、窗位 [转]
一.常用CT值 CT值的含义是:每个反应管内的荧光信号达到设定的域值时所经历的循环数.研究表明,每个模板的Ct值与该模板的起始拷贝数的 对数存在线性关系,起始拷贝数越多,Ct值越小.利用已知起始拷贝数 ...
- 12.组合(Composition)
组合也是关联关系的一种特例,它体现的是一种contains-a的关系,这种关系比聚合更强,也称为强聚合:它同样体现整体与部分间的关系,但此时整体与部分是不可分的,它们具有统一的生存期,整体的生命周期结 ...