【LeetCode】015 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.
For example, given array S = [-1, 0, 1, 2, -1, -4], A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
题解:
对于i和j,在j+1至n中寻找符合条件的第三个值,一种方法是建立map映射表,利用map的find函数。对重复项的检测上使用了比较麻烦的方法。
Solution 1 (TLE)
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> vv;
vector<int> v;
int i = , j = , n = nums.size(),tar=;
unordered_map<int, int> m;
for(; i<n; i++)
//value-position
m[nums[i]] = i;
for(i=; i<n-; i++) {
for(j=i+; j<n-; j++) {
tar = - nums[i] - nums[j];
if(m.count(tar) && m[tar]>j) {
v.push_back(nums[i]);
v.push_back(nums[j]);
v.push_back(tar);
sort(v.begin(),v.end());
if(find(vv.begin(),vv.end(),v) == vv.end())
vv.push_back(v);
v.clear();
}
}
}
return vv;
}
};
Solution 1 TLE的原因就在于重复字段的判断上,耗费了大量时间。那么怎么降低呢?这里有个小技巧,对于存在可重复数字的数组,往往联想到先将数组排序,这样可能会减少大量工作。于是在for循环前先将nums排序,然后在循环中对于两元素值相同的情况直接跳过,即当nums[i] == nums[i-1]时,由于nums[i-1]已经遍历完成,故跳过nums[i],对于j(尾指针)也是同样的处理。
Solution 2 (312ms)
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> vv;
vector<int> v;
int i = , j = , n = nums.size(),tar=;
unordered_map<int, int> m;
sort(nums.begin(),nums.end());
for(; i<n; i++)
//value-position
m[nums[i]] = i;
for(i=; i<n-; i++) {
if (i> && nums[i-]==nums[i]) continue;
for(j=i+; j<n-; j++) {
if (j>i+ && nums[j-]==nums[j]) continue;
tar = - nums[i] - nums[j];
if(m.find(tar) != m.end() && m[tar]>j) {
vv.push_back({nums[i],nums[j],tar});
}
}
}
return vv;
}
};
Solution 2 虽然AC了,但用时312ms,效率太低了。我们进一步进行优化。此题的思路就是在i之后的数组内寻找符合条件的两个值,Solution 1和Solution 2是将i和j固定,寻找第三个值,那么我们也可以固定i,寻找j和k来满足条件。还是先将数组排序,固定i后,j,k分别为剩余数组的头指针、尾指针,在j<k时执行搜索,直到j>=k时停止搜索。遇到重复项依然是跳过处理。
Solution 3 (119ms)
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &nums) {
vector<vector<int>> vv;
sort(nums.begin(), nums.end());
int n = nums.size();
for(int i=; i<n-; i++) {
if(nums[i] > ) break;
if(i> && nums[i] == nums[i-]) continue;
int a = nums[i];
int j = i+, k = n-;
while(j<k) {
int b = nums[j], c = nums[k];
if(a+b+c == ) {
vv.push_back({a,b,c});
while(j<n && nums[j] == nums[j+]) j++; j++;
while(k>i && nums[k] == nums[k-]) k--; k--;
}
else if(a+b+c > ) {
while(k>i && nums[k] == nums[k-]) k--; k--;
}
else {
while(j<n && nums[j] == nums[j+]) j++; j++;
}
}
}
return vv;
}
};
在重复项检测上也可以利用set的特性:不包含重复项。这只是一个小技巧。
Solution 4 (279ms)
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &nums) {
set<vector<int>> sv;
sort(nums.begin(), nums.end());
int n = nums.size();
for(int i=; i<n-; i++) {
if(nums[i] > ) break;
int a = nums[i];
int j = i+, k = n-;
while(j<k) {
int b = nums[j], c = nums[k];
if(a+b+c == ) {
sv.insert({a,b,c});
j++;
k--;
}
else if(a+b+c > ) k--;
else j++;
}
}
return vector<vector<int>> (sv.begin(),sv.end());
}
};
【LeetCode】015 3Sum的更多相关文章
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- 【LeetCode】16. 3Sum Closest 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...
- 【LeetCode】259 3Sum Smaller
题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...
- 【LeetCode】16. 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 【LeetCode】15. 3Sum 三个数和为0
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
- 【leetcode】15. 3Sum
题目描述: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...
- 【LeetCode】016 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 【leetcode】923. 3Sum With Multiplicity
题目如下: Given an integer array A, and an integer target, return the number of tuples i, j, k such tha ...
随机推荐
- 打字母的游戏&Java入门
目标: 在一个窗体上随机掉落字母,通过键盘输入,敲对后消除并且累计积分,否则扣除一定积分. 具体内容: 画一个窗体——>产生随机字母——>接受键盘输入——>显示分数 代码: pack ...
- 在普通网页中如何调用html5+的plus对象
<script> //用法: HTML5+的plus对象,必须由click事件触发后,才能在普通网页中使用.所以在没有click的情况下,调用本文件可以解决问题! //在代码中使用: pl ...
- ElasticSearch(二十六)修改分词器及定制自己的分词器
1.默认的分词器 standard 分词器 standard tokenizer:以单词边界进行切分standard token filter:什么都不做lowercase token filter: ...
- 教你管理SQL数据库系列(1-4)
原文 教你管理 SQL Server 数据库(1)数据库的结构 http://bbs.51cto.com/thread-1084951-1.html教你管理 SQL Server 数据库(2)系统数 ...
- 在线工具集合(新增cron quartz表达式在线生成……)
缘起 平时工作,须要一些工具.经过一些使用,对照,保留一些比較方便好用的在线工具 工具会持续更新中.. . 在线编译&&反编译 http://www.showmycode.com/ ...
- ABAP内表数据做层次XML输出
*&---------------------------------------------------------------------**& Report Z_BARRY ...
- (3)mac下"-bash: mysql: command not found"解决方案
针对 mysql: command not found 输入命令 $ ln -s /usr/local/mysql/bin/mysql /usr/bin 假如你人品不好,被打脸了,提示你权限不够: l ...
- python基础6 ----python函数
python函数 一.函数的参数 1.函数的参数从调用的角度来讲可以分为形式参数和实际参数,也可叫形参和实参. 形参:变量只有在被调用时才分配内存单元,在调用结束时,即刻释放所分配的内存单元. ...
- eclipse中,项目无法在tomcat中发布(project facet java version 1.7 is not supported)
在tomcat中发布项目时无法添加项目,错误信息:project facet java version 1.7 is not supported,如下图 这是由于你的tomcat的jdk版本低于你项目 ...
- Java EE规范是如何制定的
我们经常做的JAVA EE开发实际上都是遵从了JAVA EE的规范进行的,那么这些规范是如何制定的呢? 这就不得不提到JCP这个组织了. JCP( Java Community Process)可以翻 ...