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. 【HDOJ】3184 All Can Do

    简单数学题. #include <cstdio> #include <cstring> #include <cstdlib> int main() { int t; ...

  2. SDL2.0 学习笔记-1 windows下的第一个测试程序

    SDL全称是Simple DirectMedia Layer,是一个开源的.跨平台(win32,linux,mac)的多媒体开发c语言库. 官方网站 http://www.libsdl.org/ 第一 ...

  3. 【剑指offer】面试题24:二叉搜索树的后序遍历序列

    题目: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 思路: 递归 注意,主要就是假定数组为空时结果为fa ...

  4. UVa 1366 - Martian Mining (dp)

    本文出自   http://blog.csdn.net/shuangde800 题目链接: 点击打开链接 题目大意 给出n*m网格中每个格子的A矿和B矿数量,A矿必须由右向左运输,B矿必须由下向上运输 ...

  5. 【转】被误解的MVC和被神化的MVVM

    被误解的MVC和被神化的MVVM 作者 唐巧 发布于 2015年11月2日 | ArchSummit全球架构师峰会(北京站)2016年12月02-03日举办   被误解的 MVC MVC 的历史 MV ...

  6. JScript_Test

    Hello SyntaxHighlighter function helloSyntaxHighlighter() { return "hi!"; } function hello ...

  7. mongodb的应用场景

    这篇文章总结的比较到位:http://www.tuicool.com/articles/YnmaAj

  8. Jsp中response对象的所有属性

    所属接口:javax.servlet.http.HttpServletResponse,其父接口是ServletResponse,而且ServletResponse也现在只有唯一一个HttpServl ...

  9. codeforces Arrival of the General 题解

    A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command o ...

  10. Linux内核如何启动并装载一个可执行程序

    2016-04-07 张超<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000#/info 一.理解编译链接的 ...