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)

public class Solution {
public List<List<Integer>> threeSum(int[] num) {
Set<List<Integer>> set=new HashSet<List<Integer>>();
Arrays.sort(num);
List<List<Integer>> ans=new ArrayList<List<Integer>>();
if(num.length<=2)
{
return ans;
}
for(int i=0;i<num.length-2;i++)
{
if(i>0&&num[i]==num[i-1])
{
continue;
}
int j=i+1;
int k=num.length-1;//j,k双指针,巧妙降低复杂度至N^2
while(j<k)
{
int sum=num[i]+num[j]+num[k];
if(sum==0)
{
List<Integer> list=new ArrayList<Integer>();
list.add(num[i]);
list.add(num[j]);
list.add(num[k]);
set.add(list);
j++;k--;
}
else if(sum<0)
{
j++;
}
else
{
k--;
}
}
}
ans.addAll(set);//这个函数不错,可以把set中的元素全都添加到List。List的函数addAll(<Collection>)
return ans;
}
}

3Sum——leetcode的更多相关文章

  1. 3Sum - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 3Sum - LeetCode 注意点 和two sum那道题不一样的是这题返回的是具体的数字,不是下标 解法 解法一:将每个数字都作为target,剩下 ...

  2. leetcode 15 3sum & leetcode 18 4sum

    3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...

  3. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  4. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

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

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

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

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

  8. LeetCode 3Sum Smaller

    原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...

  9. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

随机推荐

  1. Class PLBuildVersion is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibr

    网上找了一大堆,没有解决的办法 ,主要是iOS10的适配问题,info.plist里没有加对. 访问相册,我只加了 <!-- 相册 --> <key>NSPhotoLibrar ...

  2. Beanutils基本用法

    Beanutils用了魔术般的反射技术,实现了很多夸张有用的功能,都是C/C++时代不敢想的.无论谁的项目,始终一天都会用得上它.我算是后知后觉了,第一回看到它的时候居然错过. 1.属性的动态gett ...

  3. jquery numberbox赋值

    numberbox不能使用$('#id').val( '');只能使用$('#id').numberbox('setValue','');

  4. 如何修改Xampp中MySQL的root密码?

    MySQL 的“root”用户默认状态是没有密码的,所以在 PHP 中您可以使用 mysql_connect("localhost","root"," ...

  5. 如何删除PHP数组中的元素,并且索引重排(unset,array_splice)?

    如果要在某个数组中删除一个元素,可以直接用的unset,但是数组的索引不会重排: <?php $arr = array('a','b','c','d'); unset($arr[1]); pri ...

  6. PHP读写大“二进制”文件,不必申请很大内存(fopen、fread、fwrite、fclose)

    <?php /** * 读写大二进制文件,不必申请很大内存 * 只有读取到内容才创建文件 * 保证目录可写 * * @param string $srcPath 源文件路径 * @param s ...

  7. Unix/Linux进程间通信(一):概述

    序 Linux下的进程通信手段基本上是从Unix平台上的进程通信手段继承而来的.而对Unix发展做出重大贡献的两大主力AT&T的贝尔实验室及BSD(加州大学伯克利分校的伯克利软件发布中心)在进 ...

  8. 第3月第2天 find symbolicatecrash 生产者-消费者 ice 引用计数

    1.linux find export find /Applications/Xcode.app/ -name symbolicatecrash -type f export DEVELOPER_DI ...

  9. linux常用命令-帮助命令man,whatis,apropos,info,help

    man 命令 man 配置文件,注意这里只需要写文件名称就可以了,不能写文件的绝对路径 man既可以查看命令的帮助信息也可以查看配置文件的帮助信息,如果内容太多,可以输入"/内容" ...

  10. python的os模块

    1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 2.返回指定目录下的所有文件和目录名:os.listdir() 3.函数用来删除一个文件:os.remove() ...