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. 如何同时打开两个excel

    1. 打开一个excel1 2. 不要双击想要打开的excel2.右键excel应用的图标,选择excel2007. 3. 将excel2拖动到2所打开的新建excel中. 4. over.

  2. linux 命令行 光标移动技巧

    linux 命令行 光标移动技巧 看一个真正的专家操作命令行绝对是一种很好的体验-光标在单词之间来回穿梭,命令行不同的滚动.在这里强烈建立适应GUI节目的开发者尝试一下在提示符下面工作.但是事情也不是 ...

  3. Debugging D Program on Windows

    http://x64dbg.com/ http://www.ollydbg.de/version2.html

  4. struts2的DevMode模式

    在实际应用开发或者是产品部署的时候,对应着两种模式: 1.开发模式(devMode),此时,DevMode=true 2.产品模式(proMode),此时,DevMode=false 在一些服务器或者 ...

  5. redis3.2 最新版本启动配置文件redis.conf详细说明

    Redis.conf文件内容详细说明: # 默认redis不是以后台进程的方式启动,如果需要在后台运行,需要将这个值设置成yes # 以后台方式启动的时候,redis会写入默认的进程文件/var/ru ...

  6. ActiveMQ结合Spring开发

    ---------------------------------------------------------------------------- Spring结合ActiveMQ开发 : 发送 ...

  7. 大熊君大话NodeJS之------Connect中间件模块(第一季)

    一,开篇分析 截止到今天来说,NodeJS系列文章已经有将近十篇了,让我们回顾一下: (1),大熊君大话NodeJS之开篇------Why NodeJS(将Javascript进行到底) (2),大 ...

  8. cf723b Text Document Analysis

    Modern text editors usually show some information regarding the document being edited. For example, ...

  9. 如何学习JavaScript

    Javascript是我大学里面做网站兴趣,加上进一年维护公司javascript相关的框架. 顺便回顾一下自己学习 javascript 的相关方法和技巧,分享给需要的朋友. 1.base 基础.兼 ...

  10. php.ini 安全配置

    (1) 打开php的安全模式 php的安全模式是个非常重要的内嵌的安全机制,能够控制一些php中的函数,比如system(),同时把很多文件操作函数进行了权限控制,也不允许对某些关键文件的文件,比如/ ...