【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 ...
随机推荐
- Linux 服务器上建立用户并分配权限
查看用户 whoami #要查看当前登录用户的用户名 who am i #表示打开当前伪终端的用户的用户名 who mom likes who 命令其它常用参数 参数 说明 -a 打印能打印的全部 - ...
- [php]Maximum function nesting level of '100' reached错误
今天在做后台一个模块的时候报出了这个错误. Maximum function nesting level of '100' reached 仔细分析之后发现是在类的初始化过程中(__construct ...
- make编译一
在C和C++中,首先要把源文件编译成中间代码文件,在windows下就是obj文件,linux下就是.o文件:object file.这个动作叫做编译,然后再把大量的object file合成执行文件 ...
- Data Structure Trie: suffix problem
http://www.geeksforgeeks.org/suffix-array-set-1-introduction/ http://www.geeksforgeeks.org/pattern-s ...
- Vue mixins extends extend components
mixins 调用方式: mixins: [mixin1, mixin2] 是对父组件的扩充,包括methods.components.directive等... 触发钩子函数时,先调用mixins的 ...
- ps 切片 蓝色 灰色 小标志 什么意思
切片颜色 区分自动切片与用户切片和基于图层的切片.默认情况下,用户切片和基于图层的切片带蓝色标记,而自动切片带灰色标记.-----用户切片就是你需要的切片,其他区域会形成自动切片,可以视为自己不要的, ...
- HDU 1533 Going home
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- C# 中获取CPU序列号/网卡mac地址
1.cpu序列号2.mac序列号3.硬盘id在给软件加序列号时这三个应该是最有用的,可以实现序列号和机器绑定,对保护软件很有好处.哈哈. using System; using System.Ma ...
- Linux查看端口占用进程
Linux查看端口占用进程 netstat -anlp|grep 8081 tcp /java 此处3195为进程ID
- 2.微信小程序-B站:需要先知道这些
文件结构 小程序包含一个描述整体程序的 app 和多个描述各自页面的 page.一个小程序主体部分由三个文件组成,必须放在项目的根目录,如下: 文件 必须 作用 app.js 是 小程序逻辑 app. ...