题目描述

给出一个有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. .NET Standard SDK 样式项目中的目标框架

    系列目录     [已更新最新开发文章,点击查看详细] 包表示形式 .NET Standard 引用程序集的主要分发载体是 NuGet 包. 实现会以适用于每个 .NET 实现的各种方式提供. NuG ...

  2. (OK) Android内核(4.9)集成最新版MPTCP---成功

    Android内核(4.9)集成最新版MPTCP---成功

  3. Linux就该这么学28期——Day05 vim编辑器与Shell命令脚本 (yum配置 网卡配置)

    vim 三种模式: 命令模式 按行操作 dd 剪切.删除 5dd dG   全删 yy 复制光标所在行 p 粘贴 u 撤销操作 / 搜索 /ab n  下一个 N   上一个 输入模式 a 当前光标处 ...

  4. 转一个veth的文章

    这篇写的很好,清晰明白,保存一下https://www.cnblogs.com/bakari/p/10613710.html

  5. 增强for循环的用法

    一.增强for循环 增强for循环的作用: 简化迭代器的书写格式.(注意:增强for循环的底层还是使用了迭代器遍历.)增强for循环的适用范围: 如果是实现了Iterable接口的对象或者是数组对象都 ...

  6. 4~20mA信号采集

    4-20mA信号采集 4-20mA信号采集可选卓岚ZLAN6802(485)/ZLAN6842(以太网)/ZLAN6844(无线wifi)他们不仅可以可采集4~20mA还可以采集 /0~5V/0~10 ...

  7. 串口wifi

    串口wifi 串口WiFi ZLAN7146是一款wifi转串口的wifi串口服务器.该串口服务器可以方便地使得串口设备连接到WIFI无线网络,实现串口设备的无线化网络升级.RS232接口支持全双工. ...

  8. Git的介绍以及安装

    Git的简单介绍 Git是一个开源的分布式版本控制系统,可以有效,高速的处理从很小到非常大的项目管理,GIT是为了帮助linux内核开发而开发的一个开放源码的版本控制软件 Git的安装 Linux平台 ...

  9. Pyrhon-OS库

    import os 导入OS模块 print(os.getcwd()) #获取当前路径 print(os.listdir()) #当前路径下文件信息 关于路径 Windows 下的路径采用 (\) 在 ...

  10. Gitlab 11.9.1 高可用教程

    Gitlab 11.9.1 高可用教程 一. PostgreSQL数据迁移 由于默认Gitlab的安装会内置Postgres数据库,并且没有对外,所以我们需要通过设置对应的Gitlab的配置将其中的数 ...