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][ ...
- KM算法详解+模板
http://www.cnblogs.com/wenruo/p/5264235.html KM算法用来求二分图最大权完美匹配. 本文配合该博文服用更佳:趣写算法系列之--匈牙利算法 现在有N男N女,男 ...
- GLSL Notes
[GLSL Notes] API of shader: glCreateShader(), glShaderSource(), glCompileShader(), glGetShadrInfoLog ...
- Java 理论与实践: 非阻塞算法简介——看吧,没有锁定!(转载)
简介: Java™ 5.0 第一次让使用 Java 语言开发非阻塞算法成为可能,java.util.concurrent 包充分地利用了这个功能.非阻塞算法属于并发算法,它们可以安全地派生它们的线程, ...
- php,Allowed memory size of 8388608 bytes exhausted (tried to allocate 1298358 bytes)
修改apache上传文件大小限制 PHP上传文件大小限制解决方法: 第一: 在php.ini里面查看如下行: upload_max_filesize = 8M post_max_size = 1 ...
- Objc基础学习记录--UIViewController
多个view之间切换用Embed in -->UINavigationController sugues的方式 有push和modal 及其cuestom 关于其重要的方法: -(void) ...
- (剑指Offer)面试题33:把数组排成最小的数
题目: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 思路: 1.全 ...
- Js制作点击输入框时默认文字消失的效果
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-02-17) 为了提高用户体验和易用度,一些设计师会对网页中用户经常用的东西进行优化,比如输入框.一般的输入框是怎样优化的呢 ...
- .NET开发中的事务处理大比拼
本文转载:http://www.cnblogs.com/jhxk/articles/2696307.html http://liubaolongg.blog.163.com/blog/static/2 ...
- JS函数的定义与调用方法
JS函数调用的四种方法:方法调用模式,函数调用模式,构造器调用模式,apply,call调用模式 1.方法调用模式:先定义一个对象,然后在对象的属性中定义方法,通过myobject.property来 ...