1. Question

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: 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]

]

2. Solution

  1. 先排序,然后对于每一个nums[i],在其后找到两个数字num[j]和num[k],j<k,使得三者和为0。

  2. 去掉重复的组合,如果满足三者为0,那么j需要后移到一个不同的数,k也需要前移到一个不同的数。

  3. 重复的nums[i]也需要去掉,因为nums[i]和nums[i+1] 都是和其后的进行组合,如果两个一样的话,就重复了,需要去掉。

3. Code

class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
if (nums.size() <= 2)
return vector<vector<int>>();
sort(nums.begin(), nums.end());
vector<vector<int>> res;
for (int i = 0; i < nums.size() - 2; i++) {
int start = i + 1;
int end = nums.size() - 1;
while (start < end) {
int tmp = nums[start] + nums[end];
int target = -nums[i];
if (tmp == target) {
vector<int> ele;
ele.push_back(nums[i]);
ele.push_back(nums[start]);
ele.push_back(nums[end]);
res.push_back(ele); // 去掉start重复的
while (start < end && nums[start] == ele[1])
start++;
// 去掉end重复的
while (end > start && nums[end] == ele[2])
end--;
} else if (tmp > target)
end--;
else
start++;
}
// 去掉nums[i]重复的
while (i + 1 < nums.size() - 2 && nums[i + 1] == nums[i])
i++;
}
return res;
}
};

LeetCode——3Sum的更多相关文章

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

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

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

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

  4. LeetCode 3Sum Smaller

    原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...

  5. leetcode — 3sum

    import java.util.*; /** * Source : https://oj.leetcode.com/problems/3sum/ * * Created by lverpeng on ...

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

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

  9. leetcode—3sum

    1.题目描述 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  10. Leetcode 3Sum Closet

    二手和3Sum像几乎相同的想法.二进制搜索.关键修剪.但是,在修剪做出很多错误. 然后还有一个更加速了原来的想法O(n^2). #include<iostream> #include &l ...

随机推荐

  1. CORBA(Common Object Request Broker Architecture,公共对象请求代理体系结构,通用对象请求代理体系结构)是由OMG组织制订的一种标准的面向对象应用程序体系规范

    CORBA(Common Object Request Broker Architecture,公共对象请求代理体系结构,通用对象请求代理体系结构)是由OMG组织制订的一种标准的面向对象应用程序体系规 ...

  2. pipreqs

    安装:pip3 install pipreqs 作用:帮你检测当前程序所有的安装模块,并输入到requirements.txt 执行:pipreqs ./  (必须在当前程序目录下) pycharm会 ...

  3. 【JVM】线上应用故障排查

    高CPU占用 一个应用占用CPU很高,除了确实是计算密集型应用之外,通常原因都是出现了死循环. 根据top命令,发现PID为28555的Java进程占用CPU高达200%,出现故障. 通过ps aux ...

  4. linux下安装mysql(mariadb)

    yum安装软件(官网很慢) yum install mariadb 发现版本如下,版本特别低,且安装包特别小, mariadb x86_64 :-.el7_5 base 8.9 M .我们可以配置ma ...

  5. 目标检测-Faster R-CNN

    [目标检测]Faster RCNN算法详解 Ren, Shaoqing, et al. “Faster R-CNN: Towards real-time object detection with r ...

  6. 日期格式私人定制——SimpleDateFormat

    [前言] 最近项目需要特殊的日期格式,又恰好是String类型的,以前都没怎么用到SimpleDateFormat这个类去格式化日期,脑子里蹦出来的思路就是先把Date给toString了,然后慢慢切 ...

  7. Spark的集群管理器

    上篇文章谈到Driver节点和Executor节点,但是如果想要运行Driver节点和Executor节点,就不能不说spark的集群管理器.spark的集群管理器大致有三种,一种是自带的standa ...

  8. python logging模块介绍

    1.日志级别 日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRITICAL. DEBUG:详细的信息,通常只出现在诊断问题上 INFO:确认一切按预期运行 ...

  9. 百度NLP二面

    实验室项目:1.实验室方向 2.用两分钟介绍自己的项目,创新点在哪里 个人项目:     1.自己实现的贝叶斯分类器,目的,怎么做的 2.怎么计算各个分类的先验.(因为我使用的训练预料是每个分类10篇 ...

  10. linux rm指定的文件

    如何删除一个目录下的除了想要的文件之外的所有文件 rm `ls | grep -v "aa"` Linux下 报错“命令参数列表过长”,在用mv命令一次移动3万多个文件时失败了,原 ...