Given an array S of n integers, are there elements a, b, c in S such that a + b + c = ? 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 = {- - -}, A solution set is:
(-, , )
(-, -, )

双指针的思想。使用DFS球排列组合时去重复的思想去重复

class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int>> res;
sort(num.begin(), num.end());
int len = num.size();
if(len < ) return res; for(int i = ; i <= len - ; ++i)
{
while(i> && i <= len - && num[i] == num[i-]) ++i;
if(i > len -) break;
int low = i + ;
int high = len -;
while(low < high){ int sum = num[i] + num[low] + num[high];
if(sum == ){
vector<int> tp;
tp.push_back(num[i]);
tp.push_back(num[low]);
tp.push_back(num[high]);
res.push_back(tp);
++low;
while(low < high && num[low] == num[low-])++low;
--high;
while(low <high && num[high] == num[high+]) --high;
}else if(sum < ){
++low;
while(low < high && num[low] == num[low-])++low;
}else{
--high;
while(low < high && num[high] == num[high+]) --high;
}
}
} return res;
}
};

LeetCode_3 sum的更多相关文章

  1. LeetCode_3 sum closet

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  2. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  3. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  4. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  5. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  6. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

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

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

  8. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  9. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

随机推荐

  1. 动态共享库(so)开发精悍教程

    动态共享库(so)开发精悍教程 翻译并根据实际情况进行了小小修改,仅关注Linux下动态共享库(Dynamic shared library .so)的开发. 1 简单的so实例 源文件 //test ...

  2. 重温Java的类加载机制

    原文地址:http://blog.csdn.net/hitxueliang/article/details/19992851 首先简要的说一下类加载器   我们知道,虚拟机的指令存储在以.class为 ...

  3. gcc编译时头文件库文件搜索顺序(转)

    原文: http://blog.csdn.net/silentfly1987/article/details/6119195

  4. C程序设计语言--指针和引用的区别

    在看了一篇文章以后,http://coolshell.cn/articles/7992.html,说的是C和C++之间的缺陷,当然这篇文章说的非常高深了.所以就找了一些资料,分析了这两者的区别 在&l ...

  5. Java 实现Md5算法

    package other; import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/* * ...

  6. Mapreduce运行过程分析(基于Hadoop2.4)——(一)

    1 概述 该瞅瞅MapReduce的内部执行原理了,曾经仅仅知道个皮毛,再不搞搞,不然怎么死的都不晓得.下文会以2.4版本号中的WordCount这个经典样例作为分析的切入点.一步步来看里面究竟是个什 ...

  7. Android实现图片放大缩小

    package com.min.Test_Gallery; import android.app.Activity; import android.graphics.Bitmap; import an ...

  8. iOS之AFN错误代码1016(Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable)

    请参考这篇博客:点击查看

  9. (转)WITH (NOLOCK)

    缺点: 1.会产生脏读 2.只适用与select查询语句 优点: 1.有些文件说,加了WITH (NOLOCK)的SQL查询效率可以增加33%. 2.可以用于inner join 语句 脏读: 一个用 ...

  10. 学习Android MediaPlayer

    Android Media Playback 原文 The Android multimedia framework includes support for playing variety of c ...