题目描述

给出一个有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. Spring Boot第七弹,别再问我拦截器如何配置了!!!

    持续原创输出,点击上方蓝字关注我吧 前言 上篇文章讲了Spring Boot的WEB开发基础内容,相信读者朋友们已经有了初步的了解,知道如何写一个接口. 今天这篇文章来介绍一下拦截器在Spring B ...

  2. ASP。NET MVC (NetCore 2.0)用于处理实体框架、DbContexts和对象的通用控制器和视图

    下载source - 1.5 MB 介绍 本文的源代码已更新到NetCore 2.0 ASP.净MVC项目. 当我们开始开发一个ASP.在Microsoft Visual Studio中,我们发现通过 ...

  3. .NET 云原生架构师训练营(模块一 架构师与云原生)--学习笔记

    目录 什么是软件架构 软件架构的基本思路 单体向分布式演进.云原生.技术中台 1.1 什么是软件架构 1.1.1 什么是架构? Software architecture = {Elements, F ...

  4. gitlab-centos的安装

    一:gitlab-CentOS的安装  1. 环境准备 1 [root@1-231 ~]# cat /etc/redhat-release 2 CentOS Linux release 7.4.170 ...

  5. mysql通配符_,%查询

    模糊查询 在使用模糊查询的时候,mysql使用的是最左原则,所以模糊查询语句: select * from sys_user where user_name like '#{userName}%' 我 ...

  6. DM9000网卡驱动分析(转)

    s3c6410自带的DM9000网卡驱动也是基于platform设备模型. 其定义的设备资源在arch/arm/mach-s3c64xx/mach-smdk6410中.有网卡的resource res ...

  7. zoookeeper集群和kafka集群启动快速启动脚本

    kafka.sh port=9092 # 根据端口号去查询对应的PID pid=$(netstat -nlp | grep :$port | awk '{print $7}' | awk -F&quo ...

  8. spring boot:redis+lua实现顺序自增的唯一id发号器(spring boot 2.3.1)

    一,为什么需要生成唯一id(发号器)? 1,在分布式和微服务系统中, 生成唯一id相对困难, 常用的方式: uuid不具备可读性,作为主键存储时性能也不够好, mysql的主键,在分库时使用不够方便, ...

  9. centos8安装fastdfs6.06集群方式三之:storage的安装/配置/运行

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

  10. JS实现鼠标移入水波效果

    前言 最近比较沉迷JS,所以我现在来做个鼠标的交互效果 HTML <div style="border-radius;position:relative;width:800px;hei ...