题目描述

给出一个有n个元素的数组S,S中是否有元素a,b,c满足a+b+c=0?找出数组S中所有满足条件的三元组。

注意:

  1. 三元组(a、b、c)中的元素必须按非降序排列。(即a≤b≤c)
  2. 解集中不能包含重复的三元组。
    例如,给定的数组 S = {-1 0 1 2 -1 -4},解集为(-1, 0, 1) (-1, -1, 2)

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:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
  • 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)

class Solution {
public:
    vector<vector<int> > threeSum(vector<int> &nums) {
        vector<vector<int>> res;
        sort(nums.begin(),nums.end());
        for (int k=0;k<nums.size();++k){
            if (nums[k]>0)break;
            if (k>0 && nums[k]==nums[k-1]) continue;
            int target=0-nums[k];
            int i=k+1,j=nums.size()-1;
            while (i<j){
                if (nums[i]+nums[j]==target){
                    res.push_back({nums[k],nums[i],nums[j]});
                    while (i<j && nums[i]==nums[i+1]) ++i;
                    while (i<j && nums[j]==nums[j-1]) --j;
                    ++i;--j;
                    
                }else if (nums[i]+nums[j]<target)++i;
                   else --j;
            }
        }
        return res;
    }
};
class Solution {
public:
    vector<vector<int> > threeSum(vector<int> &num) {
        sort(num.begin(),num.end());
        vector <vector<int>> ans;
        for (int i=0;i<num.size();i++){
            if (i==0 || num[i]!=num[i-1]){
                int left=i+1,right=num.size()-1;
                while (left<right){
                    while (left<right && num[i]+num[left]+num[right]>0) right--;
                    if (left<right && num[i]+num[left]+num[right]==0){
                        vector<int> temp(3);
                        temp[0]=num[i];
                        temp[1]=num[left];
                        temp[2]=num[right];
                        ans.push_back(temp);
                        while (left<right && num[left]==temp[1]) left++;
                    }else {
                        left++;
                    }
                    }
                }
            }
        
return ans;
    }
};

leetcode134:3sum的更多相关文章

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

  2. 3Sum algorithm - 非常容易理解的实现 (java)

    原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...

  3. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  4. [LeetCode] 3Sum Closest 最近三数之和

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

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

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

  7. LeetCode:3Sum, 3Sum Closest, 4Sum

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  8. 16. 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 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

随机推荐

  1. Python 中 pip 工具的安装与使用

    pip 是 Python 包管理工具,该工具提供了对Python 包的查找.下载.安装.卸载的功能. 目前如果你在 python.org 下载最新版本的安装包,则是已经自带了该工具. Python 2 ...

  2. CentOS Linux 修改主机名

    一.CentOS5 修改主机名 二.CentOS6 修改主机名 三.CentOS7 修改主机名 静态的(Static hostname)        "静态"主机名也称为内核主机 ...

  3. 热力图 vue 项目中使用热力图插件 “heatmap.js”(保姆式教程)

    我现在写的这项目是用CDN引入 heatmap.js, 可根据自己项目情况使用哪种方式引入插件. 官网地址 "https://www.patrick-wied.at/static/heatm ...

  4. Python解析yaml配置文件

    1.代码测试 import yaml fd = open('fileName.yaml', 'r') dict_tmp = yaml.load(fd) fd.close() print dict_tm ...

  5. linux的bootmem内存管理

    内核刚开始启动的时候如果一步到位写一个很完善的内存管理系统是相当麻烦的.所以linux先建立了一个非常简单的临时内存管理系统bootmem,有了这个bootmem就可以做简单的内存分配/释放操作,在b ...

  6. centos7搭建docker环境

    Docker简介 Docker是一种虚拟化技术,用来将你的应用程序及其依赖的环境一起打包成一个镜像发布,使得在任何地方都能获得相同的运行环境. Docker 是一个开源项目,诞生于 2013 年初,最 ...

  7. 源代码 VS 汇编代码 VS 目标代码 VS 字节码 VS 机器码

    1.源代码(source code) 源代码就是平时我们开发的代码:比如C.Java.Python.Shell...等 public class HelloWorld { public static ...

  8. vue知识点12

    知识点归纳整理如下: 1. 数组用下标改变,或者对象增加属性,这样的改变数据   是不能触发视图更新的,要用 Vue.set(对象,属性,值) 或this.$set(对象,属性,值) 2. this. ...

  9. dubbo配置加载优先级

    优先级从高到低: JVM 启动 -D 参数优先,这样可以使用户在部署和启动时进行参数重写,比如在启动时需改变协议的端口: XML 次之,如果在 XML 中有配置,则 dubbo.properties ...

  10. 学python,大概要多久?

    都让开!本人文科生,自学Python 2年半,作为一个曾经完全0基础,啥都不懂纯靠自学学会python的文科生,有一些不成熟的小建议可以分享一下. 首先不要觉着编程难,只要你认识26个英文字母,有一点 ...