Given an array S of n integers, are there elements abc 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[] nums) {
List<List<Integer>> list1=new ArrayList<>();
List<Integer> list=new ArrayList<>();
Map<List<Integer>,List<Integer>> map=new HashMap<>();
int a=0; Arrays.sort(nums); for(int i=0;i<=nums.length-3;i++)
{
if(nums[i]<=0)
{
list.add(nums[i]);
a=nums[i]*(-1);
}
else break;
for(int j=i+1;j<=nums.length-2;j++)
{
if(nums[j]<=a)
{
list.add(nums[j]);
a=a-nums[j];
for(int k=j+1;k<=nums.length-1;k++)
{
if(nums[k]>a)
break;
else if(nums[k]==a)
{
list.add(nums[k]);
if(!map.containsKey(list))
{
map.put(list, list);
list1.add(list);
}
break;
} }
}
else break;
list=new ArrayList<>();
list.add(nums[i]);
a=nums[i]*(-1);
}
list=new ArrayList<>();
} return list1;
}
}

15. 3Sum的更多相关文章

  1. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  2. 1. Two Sum&&15. 3Sum&&18. 4Sum

    题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up t ...

  3. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  4. leetcode 15. 3Sum 二维vector

    传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...

  5. 15. 3Sum、16. 3Sum Closest和18. 4Sum

    15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...

  6. 刷题15. 3Sum

    一.题目说明 题目非常简洁15. 3Sum,读懂题目后,理解不难. 但 实话说,我们提交代码后,Time Limit Exceeded,最主要的是给了非常长的测试用例,我本地运行后87秒,确实时间非常 ...

  7. [LeetCode] 15. 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 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  9. 15. 3Sum C++

    参考资料: https://leetcode.com/problems/3sum/discuss/7402/Share-my-AC-C%2B%2B-solution-around-50ms-O(N*N ...

  10. LeetCode——15. 3Sum

    一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...

随机推荐

  1. 键盘控制select选项上下

    $('#k').live('keydown',function(event){ if (event.keyCode==38){ /*$(this).addClass("active" ...

  2. 转: HTTP协议的头信息详解

    通常HTTP消息包括客户机向服务器的请求消息和服务器向客户机的响应消息.这两种类型的消息由一个起始行,一个或者多个头域,一个只是头域结束的空行和可 选的消息体组成.HTTP的头域包括通用头,请求头,响 ...

  3. Android 4.3正式发布:四大新功能一览

    在旧金山举行的新品发布会上,Google正式发布了Android 4.3,代号仍为“Jelly Bean”.此次更新并没有太大改变,只是紧跟4.1.4.2步伐, 新增了低功耗蓝牙.多用户登录等一系列功 ...

  4. POJ 2739 Sum of Consecutive Prime Numbers 难度:0

    题目链接:http://poj.org/problem?id=2739 #include <cstdio> #include <cstring> using namespace ...

  5. mac 安装memcached服务

    使用homebrew安装,homebrew安装方法http://brew.sh/ 安装memcached服务 brew install memcached 配置开机启动(用brew安装之后下面会提示怎 ...

  6. Spring与Hibernate整合,实现Hibernate事务管理

    1.所需的jar包 连接池/数据库驱动包 Hibernate相关jar Spring 核心包(5个) Spring aop 包(4个) spring-orm-3.2.5.RELEASE.jar     ...

  7. 虚拟机安装Centos版本的linux

    选择自定义安装,然后一路确定下去,对了,Centos的版本一定要高,比如centos 6,太低安装会出问题

  8. Android 禁用以及捕捉home键

    最近要做个小项目,其中有需要禁止home键的需求,一开始以为不可以,感觉得root一下才行,后来查了一下,发现还是不少朋友都实现了这个功能,现在也引用一下,供大家参考一下: 1. 在activity中 ...

  9. 移动设备和SharePoint 2013 - 第3部分:推送通知

    博客地址:http://blog.csdn.net/foxdave 原文地址 在该系列文章中,作者展示了SharePoint 2013最显著的新功能概观--对移动设备的支持. 该系列文章: 移动设备和 ...

  10. Python开发入门与实战2-第一个Django项目

    2.第一个Django项目 上一章节我们完成了python,django和数据库等运行环境的安装,现在我们来创建第一个django project吧,迈出使用django开发应用的第一步. 2.1.创 ...