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 < ...
随机推荐
- 《Java数据结构与算法》笔记-CH5-链表-1单链表
/** * Link节点 * 有数据项和next指向下一个Link引用 */ class Link { private int iData;//数据 private double dData;//数据 ...
- Spring中的BeanUtils与apache commons中的BeanUtils用法[1]
1. 前言 在开发过程中,经常遇到把要给一个bean的属性赋给另外一个bean.最笨的方法是每个属性都单独写一个,聪明的方法是应用反射写一个工具方法.考虑到这个需求基本每个程序员都会遇到,那么一定已经 ...
- oracle学习 七 拼接变量+日期函数(持续更)
select count(KEYCODE) from STHSGDOC.ZJSJJL where ysrq=to_date(to_char(sysdate,'yyyy')||'/1','yyyy/MM ...
- HDU 2196 Computer (树dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...
- F5 负载均衡 相关资源
F5负载均衡之检查命令的说明http://net.zdnet.com.cn/network_security_zone/2010/0505/1730942.shtml F5培训http://wenku ...
- mongodb的查询方式与sql语句对比
下面是sql和Mongodb对应的一些语法: SQL Statement Mongo Query Language Statement CREATE TABLE USERS (a Number, b ...
- Java 关于 == 和 equal()的区别
因为用new创建了两个对象,所以a和b指向两个不同的内存地址,所以返回false equal()是object的方法,所以只适用于对象,不使用于基本类型.不过equal()默认是用“==”比较两个对象 ...
- 设置文字在div中垂直居中,使用line-height
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-02-19) line-height 行间距 详细使用方法:http://www.w3school.com.cn/css/pr ...
- call()和apply()方法
还在处在刚刚学习JavaScript的初级阶段,所以理解相对浅显,是一种简单的模式理解.这里做一个笔记,让自己在回顾的时候,更加牢记. call()和apply()的形式 A.call(B," ...
- cocos2d-x 用浏览器打开网页
转自:http://www.xuebuyuan.com/1396292.html,http://www.cocoachina.com/bbs/read.php?tid=88589 First!! 源代 ...