【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 ...
随机推荐
- python接口自动化(四十二)- 项目结构设计之大结局(超详解)
简介 这一篇主要是将前边的所有知识做一个整合,把各种各样的砖块---模块(post请求,get请求,logging,参数关联,接口封装等等)垒起来,搭建一个房子.并且有很多小伙伴对于接口项目测试的框架 ...
- Windows下安装appium桌面版和命令行版
安装appium桌面版和命令行版 一 桌面版(打开很慢,常用于辅助元素定位) 1.官网下载window版本: github search appium desktop download late ...
- 【BZOJ3331】[BeiJing2013]压力 Tarjan求点双
[BZOJ3331][BeiJing2013]压力 Description 如今,路由器和交换机构建起了互联网的骨架.处在互联网的骨干位置的核心路由器典型的要处理100Gbit/s的网络流量.他们每天 ...
- 【BZOJ3791】作业 DP
[BZOJ3791]作业 Description 众所周知,白神是具有神奇的能力的.比如说,他对数学作业说一声“数”,数学作业就会出于畏惧而自己完成:对语文作业说一声“语”,语文作业就会出于畏惧而自己 ...
- spring 事物管理
示例:模拟实现转账操作,"A"转给"B"1000,"A"少1000而"B"多一千. 一.转账环境搭建 1.xml配置文件 ...
- Swift——(四)Swift中"#"的妙用
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/twlkyao/article/details/31350841 在Swift中," ...
- 找出旋转有序数列的中间值python实现
题目给出一个有序数列随机旋转之后的数列,如原有序数列为:[0,1,2,4,5,6,7] ,旋转之后为[4,5,6,7,0,1,2].假定数列中无重复元素,且数列长度为奇数.求出旋转数列的中间值.如数列 ...
- ip地址设备信息
其实是个小问题,在项目中遇到要获取手机ip地址和mac地址的问题,mac地址获取用appcan的uexDevice插件可以实现. 但是ip地址,获取的方式很多,具体有搜狐/腾讯/新浪等提供的接口.但是 ...
- iOS uitableViewCell 选中 push后返回 取消选中状态
首先我有一个UITableViewController,其中每个UITableViewCell点击后都会push另一个 ViewController,每次点击Cell的时候,Cell都会被选中,当从p ...
- crontab 定时器
cronntab 定时器 crontab -e 编辑定时器 crontab -l 查看定时器 //每十分钟执行一次 */10 * * * * curl http://xxxxx //每天 凌晨 中午1 ...