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. 二模 (5) day2

    第一题: 有 N 个人顺时针围在一圆桌上开会,他们对身高很敏感. 因此决定想使得任意相邻的两人的身高差距最大值最小. 如果答案不唯一,输出字典序最小的排列,指的是身高的排列.N<=50 解题过程 ...

  2. 数据结构-AVL树

    实现: #ifndef AVL_TREE_H #define AVL_TREE_H #include "dsexceptions.h" #include <iostream& ...

  3. WP8 独立存储 总结3(应用设置)

    •可在独立存储中使用ApplicationSettings对象•在独立存储中存储键/值对的Dictionary方式存储 •存储的对象将永久保存 在应用设置中保存数据 void saveString(s ...

  4. AOP面向切面编程

    1.AOP Aop(aspect object programming)面向切面编程 功能: 让关注点代码与业务代码分离! 关注点 重复代码就叫做关注点: 切面 关注点形成的类,就叫切面(类)! 面向 ...

  5. FFT快速傅立叶变换的工作原理

    实数DFT,复数DFT,FFTFFT是计算DFT的快速算法,但是它是基于复数的,所以计算实数DFT的时候需要将其转换为复数的格式,下图展示了实数DFT和虚数DFT的情况,实数DFT将时域中N点信号转换 ...

  6. SharePoint 列表应用实例 - 显示约束

    博客地址:http://blog.csdn.net/FoxDave 有时会碰到这样的需求,比如上传周报到文档库,周报只能领导和自己看到,其他同事是看不到的.通常我们开发的人遇到这种情况条件反射地想到的 ...

  7. 《用C++语言编写一个程序,求PI的值》

    //编写一个C++程序求PI的值 /* PI=16arctan(1/5)-4arctan(1/239) 其中arctan用如下形式的极数计算: arctan=x-(x^3/3)+(x^5/7)-(x^ ...

  8. SecureCRT下的串口无法输入

    用串口配置交换机的时候,出现的问题: 用secureCRT建了一个串口COM1后,连接上开发板后,可以正确接受和显示串口的输出,但是按键输入无效. 解决方法: Session Options -> ...

  9. spark与Hadoop区别

    2分钟读懂Hadoop和Spark的异同 2016.01.25 11:15:59 来源:51cto作者:51cto ( 0 条评论 )   谈到大数据,相信大家对Hadoop和Apache Spark ...

  10. php大力力 [018节]如何联系大力力

    有事儿就注册博客园,给我发 博客园站内的 短消息呗,唉,没有人联系我呀,啦啦啦,爱我爱我,快点爱我 2015-08-26 php大力力018.如何联系大力力