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. office web apps server 问题和解决办法

    New-OfficeWebAppsFarm –InternalURL "http://owa.zjkhlib.com" –AllowHttp –EditingEnabled 错误1 ...

  2. Codeforces Round #316 (Div. 2) D、E

    Problem D: 题意:给定一棵n个点树,每个点有一个字母,有m个询问,每次询问某个节点x的子树中所有深度为k的点能否组成一个回文串 分析:一堆点能组成回文串当且仅当数量为奇数的字母不多于1个,显 ...

  3. app后端设计--总目录

    做了3年app相关的系统架构,api设计,先后在3个创业公司中工作,经历过手机网页端,android客户端,iphone客户端,现就职于app云后端平台bmob(想了解bmob点击这里).其中的乐与苦 ...

  4. Could not Build module ImageIO

    错误提示:Could not Build module ImageIO: 原因:修改了XCode API 源文件 措施:从一个干净的未曾修改过的XCode中拷贝一份相应的文件添加到发生错误的XCode ...

  5. HDOJ 5093 Battle ships 二分图匹配

    二分图匹配: 分别按行和列把图展开.hungary二分图匹配. ... 例子: 4 4 *ooo o### **#* ooo* 按行展开. .. . *ooo o#oo oo#o ooo# **#o ...

  6. hdu2795Billboard(线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=2795 单点更新,树存储的为某一行内剩余的长度 // File Name: hdu2795.cpp // Auth ...

  7. Linux安装配置php

    1.获取安装文件: http://www.php.net/downloads.php  php-5.3.8.tar.gz 获取安装php需要的支持文件:http://download.csdn.net ...

  8. [Redux] React Todo List Example (Toggling a Todo)

    /** * A reducer for a single todo * @param state * @param action * @returns {*} */ const todo = ( st ...

  9. 大数据笔记11:MapReduce的运行流程

    1.基本概念 (1)Job & Task (2)JobTracker (3)TaskTracker

  10. 前端 JavaScript基础

    前言 JavaScript 是属于网络的脚本语言,被数百万计的网页用来改进设计.验证表单.检测浏览器.创建cookies,以及更多的应用. 一.如何编写 1.存在形式 方式一:存在js文件中,即写入j ...