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. [Skills] 在桌面打开一个BAT文件,CMD窗口不关闭

    每次开机都要取得本机IP,然后远程连接上去,屏幕太小,不好输入,想写个bat,执行就能看到IP,并且停留在cmd窗口上,想来简单,以前搜了好久没找到好的办法,今天找到一个贴子,竟然可以,呵呵!   以 ...

  2. C语言字符串长度(转)

    C语言字符串长度的计算是编程时常用到的,也是求职时必考的一项. C语言本身不限制字符串的长度,因而程序必须扫描完整个字符串后才能确定字符串的长度. 在程序里,一般会用strlen()函数或sizeof ...

  3. CoreLocation 下的定位跟踪测速

    #import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewCont ...

  4. Arduino101学习笔记(十四)—— Flash库

    一.一些API 1.打开文件 SerialFlashFile file; file = SerialFlash.open("filename.bin"); if (file) { ...

  5. Linux学习笔记(2)Linux学习注意事项

    1 学习Linux的注意事项 ① Linux严格区分大小写 ② Linux中所有内容均以文件形式保存,包括硬件,如硬件文件是/deb/sd[a-p] ③ Linux不靠扩展名区分文件类型,但有的文件是 ...

  6. UVA232字符串处理

    #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> ...

  7. 【jQuery 使用】 利用jQuery.prop("outerHTML")获取包含自身在内的HTML元素的HTML代码

    jQuery.html() 是获取当前节点下的html代码,并不包含当前节点本身的代码,然而我们有时候的确需要,可以通过jQuery.prop("outerHTML")的方式设置. ...

  8. scrollview嵌套listview 滑动事件冲突的解决方法

    listView.setOnTouchListener(new View.OnTouchListener() {                            @Override       ...

  9. AchartEngine 的学习

    第一步:我使用的事AchartEngine 1.1.0 的包.大家要先下在这个包,放到项目中,创建一个lib文件夹.然后倒金项目中去.然后再AndroidManifest.xml 中需要注册一下代码是 ...

  10. 利用JAX-WS 开发web服务

    近日在学习Rogers Candenhead的第六版的<Java 入门经典>第22章.利用JAX-WS 开发web服务,简略总结而言主要包括以下几个步骤: 1.定义服务端点接口类: 主要就 ...