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. 传引用 C(转)

    转自:http://myturn.blog.hexun.com/15584978_d.html #include <iostream> using namespace std ; void ...

  2. 在Eclipse中用图形界面的方式获取Salesforce中Object的Query语句

    对Salesforce中的Object进行相应的Query是必不可少的操作,大家可以去这个链接去看看官网的解读  http://docs.database.com/dbcom/en-us/db_sos ...

  3. scrollTo , scrollBy区别

    View视图中scrollTo 与scrollBy这两个函数的区别 . 首先 ,我们必须明白在Android View视图是没有边界的,Canvas是没有边界的,只不过我们通过绘制特定的View时对 ...

  4. poj 2115 Looooops

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23637   Accepted: 6528 Descr ...

  5. C#插件构架实战 + Visual C#插件构架实战补遗(转)

    C#插件构架实战 C# 插件构架实战 Jack H Hansen [ 2004-07-27 ] Keywords C# 插件 反射(System.Reflection) 属性(System.Attri ...

  6. java 汽车销售收入系统

     >>>>>>>>>>>>>>>>>>>> 语言:java 工具:eclipse ...

  7. 移动端网页 -- 安卓与IOS兼容

    1.在a链接长按时,ios系统会识别并复制a链接中的href值,而安卓不会,只会选择复制文字 关于长按复制其他区域内容:pc端可以实现,在移动端目前还没有找到解决方案,很多都是基于flash的 2.i ...

  8. http://www.blogjava.net/zhangchao/archive/2011/05/26/351051.html

    http://www.blogjava.net/zhangchao/archive/2011/05/26/351051.html

  9. 5分钟弄懂Docker--转载

    编者按:7月3日的“CSDN在线培训:Docker之道”,同时在线人数达到了历史新高,但是最后的QA环节,笔者发现大家的问题 还是很初级的,Docker技术还处在Gartner技术曲线的萌芽期.刚好前 ...

  10. Code Rush插件

    code rush 是微软推出的一款VS2008上的插件.他有强大的文件和代码导航功能,易于访问的重构和代码创建功能.一组编辑器.选择.剪贴板工具等. 教程链接 http://www.devexpre ...