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)

java:

public class Solution {
public List<List<Integer>> threeSum(int[] num) {
int len = num.length;
List<List<Integer>> list = new LinkedList<List<Integer>>();
if(len<3){
return list;
}
Arrays.sort(num);
int i=0;
while(i<len-2){
int s=i+1;
int e=len-1;
while(s<e){
if(num[s]+num[e]==0-num[i]){
List<Integer> lst = new LinkedList<Integer>();
lst.add(num[i]);
lst.add(num[s]);
lst.add(num[e]);
list.add(lst);
while(s+1<len&&num[s+1]==num[s]){
s++;
}
s++;
while(e-1>=0&&num[e-1]==num[e]){
e--;
}
e--;
}else if(num[s]+num[e]<0-num[i]){
s++;
}else{
e--;
}
}
while(i+1<len&&num[i+1]==num[i]){
i++;
}
i++;
}
return list;
}
}

c++:

class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int> > r;
int n=num.size();
sort(num.begin(),num.end());
if(n<3)
return r; int i=0;
while(i<n-2){
int k1 = num[i];
int s = i+1,e=n-1;
while(s<e){
if(num[s]+num[e]==0-k1){
vector<int> v;
v.push_back(k1);
v.push_back(num[s]);
v.push_back(num[e]); r.push_back(v);
while(s+1<=n-1&&num[s]==num[s+1]){
s++;
}
s++;
while(e-1>=i&&num[e]==num[e-1]){
e--;
}
e--;
}else if(num[s]+num[e]<0-k1){
s++;
}else{
e--;
}
}
while(i+1<=n-1&&num[i]==num[i+1]){
i++;
}
i++;
}
return r;
}
};

3Sum的更多相关文章

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

  2. 3Sum algorithm - 非常容易理解的实现 (java)

    原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...

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

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

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

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

  6. Leetcode 16. 3Sum Closest

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

  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. 16. 3Sum Closest

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

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

  10. No.016:3Sum Closest

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

随机推荐

  1. Open judge C16H:Magical Balls 快速幂+逆元

    C16H:Magical Balls 总时间限制:  1000ms 内存限制:  262144kB 描述 Wenwen has a magical ball. When put on an infin ...

  2. sql2014 新建用户并登陆

    EXEC master.dbo.sp_addlogin @loginame = N'testuser1', @passwd = '123456', @defdb = N'master', @defla ...

  3. 封装用className选元素

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. zookeeper源码分析三LEADER与FOLLOWER同步数据流程

    根据二)中的分析,如果一台zookeeper服务器成为集群中的leader,那么一定是当前所有服务器中保存数据最多的服务器,所以在这台服务器成为leader之后,首先要做的事情就是与集群中的其它服务器 ...

  5. 20145223《Java程序设计》第5周学习总结

    20145223 <Java程序设计>第5周学习总结 教材学习内容总结 ·由于在编程的时候会遇到因各种原因而导致的错误,于是我们可以使用"try"."catc ...

  6. 【转载】Android app 安全测试调研及执行

    本文来源于:http://testerhome.com/topics/2209 一.通过在线工具进行测试 1.腾讯金刚审计系统http://service.security.tencent.com 优 ...

  7. Storm实战:在云上搭建大规模实时数据流处理系统(Storm+Kafka)

    在大数据时代,数据规模变得越来越大.由于数据的增长速度和非结构化的特性,常用的软硬件工具已无法在用户可容忍的时间内对数据进行采集.管理和处理.本文主要介绍如何在阿里云上使用Kafka和Storm搭建大 ...

  8. Java NIO之选择器Selector

    在单独的线程中,检查多个通道是否可以进行IO操作. Selector创建:静态工厂方法创建 Selector selector = Selector.open(); 注册通道 channel.conf ...

  9. node.js 调用天气webservice接口

    首先安装soap模块 npm install soap 1 2 3 4 5 6 7 8 9 10   var soap = require('soap');   var url = 'http://w ...

  10. jquery实现隐藏,简化和更多

    HTML代码: <div class="box"> <div class="header"> <h3>图书分类</h3 ...