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 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的更多相关文章
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- 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 ...
- 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 ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- 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 = ...
- 刷题15. 3Sum
一.题目说明 题目非常简洁15. 3Sum,读懂题目后,理解不难. 但 实话说,我们提交代码后,Time Limit Exceeded,最主要的是给了非常长的测试用例,我本地运行后87秒,确实时间非常 ...
- [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 ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- 15. 3Sum C++
参考资料: https://leetcode.com/problems/3sum/discuss/7402/Share-my-AC-C%2B%2B-solution-around-50ms-O(N*N ...
- LeetCode——15. 3Sum
一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...
随机推荐
- 菜鸟开始学习SSDT HOOK((附带源码)
看了梦无极的ssdt_hook教程,虽然大牛讲得很细,但是很多细节还是要自己去体会,才会更加深入.在这里我总结一下我的分析过程,若有不对的地方,希望大家指出来.首先我们应该认识 ssdt是什么?从梦无 ...
- “Unable to execute dex: Multiple dex files”如何解决?
遇到报错: [2014-02-13 17:27:03 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/kkdia ...
- C语言中 scanf 和 printf 的小要点
1 scanf_s需指定%c 个数 h short l long关键字 * 不赋给任何值 W 指定读入宽度 转换字符 a 读浮点值(c99) A 读浮点值(c99) c 读单字符 d 读十进制数 i ...
- 安装VMWare tools,以及解决安装后/mnt中有hgfs但没共享文件的方法
一.首先是安装VMWare tools 安装过程可参考:Installing VMware Tools in an Ubuntu virtual machine 安装成功后,可看的如下信息: ...
- C#窗体的加载等待(BackgroundWorker控件)实现
窗体拉一个Button按钮和一个加载等待显示的label, label默认隐藏,点击按钮时显示这个label,加载完再隐藏 1.工具箱拉BackgroundWorker控件到窗体 2.backgrou ...
- HandleErrorAttribute 特性使用
public class EwHandleErrorAttribute : HandleErrorAttribute { public override void OnException(Except ...
- Hibernate 的配置文件
Hibernate 配置文件 •Hibernate 配置文件主要用于配置数据库连接和 Hibernate 运行时所需的各种属性 •每个 Hibernate 配置文件对应一个 Configuration ...
- java使用JDBC连接数据库
1.下载connectors/j: 地址:http://www.mysql.com/downloads/connector/j/ 选择 到此页面点击“No,thanks……" 若使用Java ...
- POJ 2992 求组合数的因子个数
求C(n,k)的因子个数 C(n,k) = (n*(n-1)*...*(n-k+1))/(1*2*...*k) = p1^k1 * p2^k2 * ... * pt^kt 这里只要计算出分子中素数因子 ...
- 推荐一款好用轻便的在线UML画图工具
刚接触UML时间不长,看了N多教学视频,下载好了几个软件各种不习惯 当我遇见了ProcessOn 从此我彻底“爱上”了它! http://www.processon.com/ UML各类例图它几乎全 ...