题目:

Given an array S of n integers, are there elements abc 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的更多相关文章

  1. 【LeetCode】15. 3Sum 三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...

  2. 【LeetCode】16. 3Sum Closest 最接近的三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...

  3. 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...

  4. 【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 ...

  5. 【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 ...

  6. 【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 ...

  7. 【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 ...

  8. 【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 ...

  9. 【leetcode】923. 3Sum With Multiplicity

    题目如下: Given an integer array A, and an integer target, return the number of tuples i, j, k  such tha ...

随机推荐

  1. NSNotificationCenter详解

    本文转载至 http://blog.csdn.net/chengyingzhilian/article/details/7874408 作用:NSNotificationCenter是专门供程序中不同 ...

  2. [转]springmvc常用注解标签详解

    1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...

  3. 逻辑英语 第四季 Speaking and Listening

    1. 发音的变革 背字典:牛津双解/朗文英汉 a. 如何一分钟变伦敦腔 发音有两种:伦敦腔/其他 生理分析: ① 后置发音:瞬间华丽变声第一步 东方人靠嘴巴发音: 西方人用胸腔发音[有共鸣] 方法1: ...

  4. 爬虫入门【2】Requests库简介

    发送请求 使用Requests发送网络请求很简单 #首先要导入requests库 import requests #返回一个Response对象 r=requests.get('https://git ...

  5. Linux安装Nginx使用负载均衡

    1.实验准备准备三台计算机 nginx1 192.168.13.121 作为nginx负载均衡器nginx2 192.168.13.24  web服务,提供一个页面        nginx3 192 ...

  6. (转)jquery $.proxy的使用

    在某些情况下,我们调用Javascript函数时候,this指针并不一定是我们所期望的那个.例如: 1 //正常的this使用 2 $('#myElement').click(function() { ...

  7. Jquery的parent和parents(找到某一特定的祖先元素)用法(转发:https://blog.csdn.net/cui_angel/article/details/7903704)

    <!-- parent是指取得一个包含着所有匹配元素的唯一父元素的元素集合. parents则是取得一个包含着所有匹配元素的祖先元素的元素集合(不包含根元素).可以通过一个可选的表达式进行筛选. ...

  8. linux 基础2-null,cut,wc,head,tail

    一. 特殊文件: /dev/null和/dev/tty Linux系统提供了两个对Shell编程非常有用的特殊文件,/dev/null和/dev/tty.其中/dev/null将会丢掉所有写入它的数据 ...

  9. Linux 下 Crontab 命令使用详解 定时任务

    一.  Crontab 介绍 crontab命令的功能是在一定的时间间隔调度一些命令的运行. 1.1 /etc/crontab 文件 在/etc文件夹下有一个crontab文件,这里存放有系统运行的一 ...

  10. Mac下XAMPP环境中安装MySQLdb

    环境: Mac OS X. Mac下安装MySQLdb模块着实多了些步骤. 用easy_install或者pip安装时有两大问题,"mysql_config not found"和 ...