题目描述

给出一个有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. SPI应用 用SPI总线读取气压传感器SCP1000的数据

    Using SPI to read a Barometric Pressure Sensor This example shows how to use the SPI (Serial Periphe ...

  2. 字节码暴力破解censum(老版本)

    声明 事先声明,本文仅提供破解方法以供个人及读者们学习Java字节码,不提倡破解程序. 本文是个人学习掘金小册张师傅的<JVM字节码从入门到精通>后,作为一个实践的记录,并无恶意. 关于c ...

  3. Oracle 数据库下赋予用户的执行存储过程和创建表权限

    grant create any table to username; grant create any procedure to username; grant execute any proced ...

  4. nginx完美支持thinkphp3.2.2(需配置URL_MODE=>3 rewrite兼容模式)

    来源:http://www.thinkphp.cn/topic/26637.html 环境:nginx 1.6,thinkphp3.2.2 第一步,修改server块 server { listen ...

  5. spring-boot-route(十五)整合RocketMQ

    RocketMQ简介 RocketMQ是阿里巴巴开源的消息中间件.目前已经贡献给Apache软件基金会,成为Apache的顶级项目. rocketMQ基本概念 1. Producer Group 生产 ...

  6. CSS的元素显示模式与转换

    CSS的元素显示模式与转换 1. CSS的元素显示模式 1.1 块元素 <div>标签是最典型的块元素.另外常见的块元素有h1~h6.p.ul.ol.li等. 特点: 独占一行 高度.宽度 ...

  7. Java第一课!

    public class Text { public static void main(String[] args) { int a=100; //赋值a=100 System.out.println ...

  8. 谈谈FTP

    一.关于FTP 1.FTP是什么? FTP,全称"文件传输协议".属于TCP/IP四层模型中的应用层. 2.TCP/IP五层模型有哪些? 如图所示: 用文字叙述(从高层到底层): ...

  9. logstash -grok插件语法介绍

      介绍 logstash拥有丰富的filter插件,它们扩展了进入过滤器的原始数据,进行复杂的逻辑处理,甚至可以无中生有的添加新的 logstash 事件到后续的流程中去!Grok 是 Logsta ...

  10. centos8安装fastdfs6.06集群方式一之:软件下载与安装

    一,查看本地centos的版本 [root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) 说 ...