给定一个包含 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. 《DSP using MATLAB》Problem 8.31

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  2. 解析Request和Response

    简介: Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. request和response对象即然代表请求和响应 ...

  3. springboot-actuator监控的401无权限访问

    在pom.xml里边添加 <dependency> <groupId>org.springframework.boot</groupId> <artifact ...

  4. pandas一些基本操作(DataFram和Series)_2

    import numpy as nparr1 = np.arange(32).reshape(8,4)print(arr1)arr1 = arr1.reshape(-1);print(arr1)arr ...

  5. PKUOJ 区间内的真素数

    http://bailian.openjudge.cn/tm2018/A/ #include <iostream> #include <math.h> #include < ...

  6. NIO的学习总结

    1.简单画的NIO流程图 2.代码实现编程: Client: package nio; import java.io.IOException; import java.net.InetSocketAd ...

  7. 2019-2-18-VisualStudio-给项目添加特殊的-Nuget-的链接

    title author date CreateTime categories VisualStudio 给项目添加特殊的 Nuget 的链接 lindexi 2019-02-18 15:56:48 ...

  8. 深度神经网络Google Inception Net-V3结构图

    深度神经网络Google Inception Net-V3结构图 前言 Google Inception Net在2014年的 ImageNet Large Scale Visual Recognit ...

  9. bzoj1003物流运输 最短路+DP

    bzoj1003物流运输 题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输 ...

  10. Django项目:CRM(客户关系管理系统)--83--73PerfectCRM实现CRM模板统一

    {#king_index.html#} {## ————————73PerfectCRM实现CRM模板统一————————#} {% extends "master/sample.html& ...