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. ok6410,mmu,内存管理

    MMU 一.MMU学习 MMU其实就是一个页表.将虚拟地址通过查表的方式,对应到物理地址去他由一个或一组芯片组成,一般存在与协处理器中. 1.将虚拟地址转化为物理地址 2.访问权限管理 1.1得出mm ...

  2. ☆ ☆ VMware9虚拟机安装MAC OS X Mountain Lion 10.8.2详细图文教程 (转)

    参考  http://diybbs.zol.com.cn/1/34037_699.html 然后对安装的Mac系统进行升级到最新版本. 安装mac系统之后,再安装VMTOOLS darwin. 方法可 ...

  3. UI中 frame 与 transform的用法与总结

    在iOS中,我们是不可以直接访问控件中frame的结构体的成员的,因此我们需要分三步来改变一个UI控件的位置,大小 一.frame用法 frame的结构体类型为: struct CGRect { CG ...

  4. Unity2D 之 Sprite点击事件

    以下方法纯属我YY,切勿当真!!! 给 Sprite添加点击事件步骤: 1. 创建一个 Sprite 2. 给Sprite添加一个 Box Collider 2D 3. 将如果脚本放到Sprite上: ...

  5. SQLServer 维护脚本分享(06)CPU

    --CPU相关视图 SELECT * FROM sys.dm_os_sys_info SELECT * FROM sys.dm_exec_sessions SELECT * FROM sys.sysp ...

  6. supervisor(一)基础篇

    这两天干的活,是让楼主写一个supervisor的listener,用来监控supervisor所管理子进程的状态,当子进程异常退出时,楼主写的这个listener将会触发报警.在这里总结下super ...

  7. 【转】html、css、js文件加载顺序及执行情况

    原链接:http://www.cnblogs.com/Walker-lyl/p/5262075.html 今天看书,看到html,css,js加载执行情况,发现自己并不是真正的了解,网上搜了半小时依然 ...

  8. Liferay 6.2 改造系列之三:删除Docbar中的添加内容功能

    在/portal-master/portal-web/docroot/html/portlet/dockbar/add_panel.jsp文件中 将以下内容: if (hasAddContentAnd ...

  9. PHP public private protected 三种修饰符的区别

    public 表示全局,类内部外部子类都可以访问:private表示私有的,只有本类内部可以使用:protected表示受保护的,只有本类或子类或父类中可以访问:

  10. CSS3-样式继承,层叠管理,文本格式化