给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

注意:答案中不可以包含重复的三元组。

例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为: [ [-1, 0, 1], [-1, -1, 2] ]

双指针加去重

class Solution {
public:
vector<vector<int> > threeSum(vector<int>& nums) {
int len = nums.size();
sort(nums.begin(), nums.end());
map<int, pair<int, int> > check;
vector<vector<int> > res;
for(int i = 0; i < len - 2; i++)
{
int low = i + 1;
int high = len - 1;
while(low < high)
{
if(nums[low] + nums[high] == -nums[i])
{
res.push_back({nums[i], nums[low], nums[high]});
//去重
while(nums[low] == nums[low + 1])
low++;
low++;
}
if(nums[low] + nums[high] > -nums[i])
{
high--;
}
else
{
low++;
}
}
//去重
while(nums[i] == nums[i + 1])
i++;
}
return res;
}
};

Leetcode15.3Sum三数之和的更多相关文章

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

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

  2. [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 ...

  3. [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 all un ...

  4. [leetcode]15. 3Sum三数之和

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  5. 【LeetCode每天一题】3Sum(三数之和)

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  6. [LintCode] 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 ...

  7. [Lintcode 3sum]三数之和(python,二分)

    题目链接:http://www.lintcode.com/zh-cn/problem/3sum/?rand=true# 用这个OJ练练python…这个题意和解法就不多说了,O(n^2lgn)就行了, ...

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

  9. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

随机推荐

  1. python 读取excel Xlrd模块

    1. 安装xlrd模块 我使用pip安装: cmd ->切换到pip安装所在路径->pip install xlrd->回车 2. 使用 2.1:打开Excel表 导入模块:  im ...

  2. java主函数参数传递args

    a.javaJava应用程序的主入口方法main(String[] args),表示该方法需要接收一个字符串数组类型的参数, 如果该参数不指定,agrs接收的是null. 程序:   public C ...

  3. 从0开始学习ssh之岗位管理

    岗位应该有如下属性,id.名称.说明.新建role.java.添加id,name,description并增加三个的get set方法. 之后建立Role.hbm.xml 并加入到applicatio ...

  4. 关于LZO无法平台上压缩,但是数据需要使用平台压缩的问题解决

    我们做hive查询时候经常会出现出数过慢的问题,于是采用了LZO压缩,再在压缩块上做索引的方式去解决这个问题,但是也引入了新的问题点 lzo本身的压缩功能只能在linux上压缩再上传到HDFS平台,供 ...

  5. Redis高级命令的使用学习

  6. day 71作业

    作业: url配置 urlpatterns = [ url(r'^v2/cars/$',views.CarAPIView.as_view()), url(r'^v2/cars/(?P<pk> ...

  7. WIN32_FIND_DATA 详细结构(附循环读取文件代码)

    //去除路径最后多余的斜杠和反斜杠 std::string TrimPath(std::string path) { //string test3("内容"); 使用引用字符数组作 ...

  8. 攻防世界wp--web新手1

    https://adworld.xctf.org.cn/task/answer?type=web&number=3&grade=0&id=5061 打开是一个网页 知识点: 根 ...

  9. Spring Cloud Eureka集群配置及注意事项(Greenwich版本)

    Spring Cloud Eureka集群配置及注意事项(Greenwich版本) 一·概述 Spring Cloud Netflix Eureka 是一个提供服务注册与发现的套件.服务提供者只需要将 ...

  10. swoole入门abc

    1. 入门abc 1.1 github账号添加 第一步依然是配置git用户名和邮箱 git config user.name "用户名" git config user.email ...