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)

分析:

这道题原本不难,但是题目要求结果不能够包含重复的 triplets, 往往容易出错。

思路1:

step 1: 先排序

step 2:   枚举前两个数,如果要满足 sum = 0, 那么第三个数必须是 前两个数和的负数。

通过二分搜索去查找。。复杂度 是 O(N^2 * lgN)

思路2:

O(N^2)的算法。

借助于 2 sum的思路:如果在一个已经排序的数组中,寻找两个数的和为给定的sum, 其实有 O(N)的算法。

指针 i, j  分别指向数组的首尾,如果 num[i] + num[j] < 0, 则 i++;  如果 > 0, j--; 直到和等于sum.

因此,本题可以

step 1:   排序

step 2:  先枚举第一个数,然后在排好序的数组中寻找 两个数和等于 第一个数的负数。

Note: 为了避免重复的 triplets, 有一些小细节需要考虑。具体见下面的代码。

基本的思想就是 如果碰到相邻的数相同,可以跳过。

class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int> > result;
if(num.size() < 3) return result; sort(num.begin(), num.end()); //处理以num[k]开头的和
for(int k=0; k<num.size()-2; ++k){
if(k > 0 && num[k] == num[k-1]) continue; //避免重复 int i=k+1, j=num.size()-1; //分别指向首尾
while(i < j){
if(i > k+1 && num[i] == num[i-1]){
++i;
continue;
}
if(j < num.size()-1 && num[j] == num[j+1]){
--j;
continue;
} if(num[k] + num[i] + num[j] < 0)
++i;
else if(num[k] + num[i] + num[j] > 0)
--j;
else{
result.push_back(vector<int>{num[k], num[i++], num[j--]});
}
}
} return result;
}
};

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

  1. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

  2. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

  3. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  4. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  5. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  6. [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  7. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  8. [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  9. [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  10. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

随机推荐

  1. 安装 request模块

    python3 requests 安装包下载安装[windows] 听语音 | 浏览:54 | 更新:2016-07-25 17:09 windows下直接使用:easy_install reques ...

  2. JavaScrip的DOM操作(13号讲)

    1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型,文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西 2.Windows对象操作 一.属性和方法 二.Window.open(& ...

  3. DBCC DBREINDEX重建索引提高SQL Server性能

    大多数SQL Server表需要索引来提高数据的访问速度,如果没有索引,SQL Server 要进行表格扫描读取表中的每一个记录才能找到索要的数据.索引可以分为簇索引和非簇索引,簇索引通过重排表中的数 ...

  4. JBoss像tomcat那样创建部署文件,JBoss创建虚拟目录

    jboss可以像tomcat那样,写一个配置文件,指向应用所在的路径,而不用将应用直接复制到deploy下的某一个以.war结尾的文件夹下吗? 答:好像是不能直接操作,但是可以通过变通的方式来搞定.在 ...

  5. mysql在一台服务器搭建主从

    注:本环境事先执行rm -rf /usr/local/mysql   以方便实验. 1. 主与从,类似于A机器和B机器的连接,通过bin_log和rpel_log 进行数据连接 2. 如图所示: 3. ...

  6. PHP+mysql常用类库

    <?php /** * @title: Ekcms mysql类库 * @version: 1.0 * @author: perry <perry@1kyou.com> * @pub ...

  7. Visual Studio 2012中的为创建类时的添加注释模板

    我们往往需要给类添加注释,我们可以把注释块复制出来,放到文件中,然后在需要的时候,复制.粘贴.这样的重复劳动增加了程序员的体力劳动,而VS中给我们提供了项模版,我们只需要在其中修改一点点模版就能达到这 ...

  8. Hibernate的generator属性之意义

    Hibernate的generator属性之意义 本文讲述Hibernate的generator属性的意义.Generator属性有7种class,本文简略描述了这7种class的意义和用法. Hib ...

  9. 如何在Hadoop的MapReduce程序中处理JSON文件

    简介: 最近在写MapReduce程序处理日志时,需要解析JSON配置文件,简化Java程序和处理逻辑.但是Hadoop本身似乎没有内置对JSON文件的解析功能,我们不得不求助于第三方JSON工具包. ...

  10. cocopods的使用方法

    虽然网上关于CocoaPods安装教程多不胜数,但是我在安装的过程中还是出现了很多错误,所以大家可以照下来步骤装一下,我相信会很好用. 前言 在iOS项目中使用第三方类库可以说是非常常见的事,但是要正 ...