3 Sum leetcode java
题目:
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)
题解:
3 Sum是two Sum的变种,可以利用two sum的二分查找法来解决问题。
本题比two sum增加的问题有:解决duplicate问题,3个数相加返回数值而非index。
首先,对数组进行排序。
然后,从0位置开始到倒数第三个位置(num.length-3),进行遍历,假定num[i]就是3sum中得第一个加数,然后从i+1的位置开始,进行2sum的运算。
当找到一个3sum==0的情况时,判断是否在结果hashset中出现过,没有则添加。(利用hashset的value唯一性)
因为结果不唯一,此时不能停止,继续搜索,low和high指针同时挪动。
时间复杂度是O(n2)
实现代码为:
1 public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
2 ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
3 if(num.length<3||num == null)
4 return res;
5
6 HashSet<ArrayList<Integer>> hs = new HashSet<ArrayList<Integer>>();
7
8 Arrays.sort(num);
9
for(int i = 0; i <= num.length-3; i++){
int low = i+1;
int high = num.length-1;
while(low<high){//since they cannot be the same one, low should not equal to high
int sum = num[i]+num[low]+num[high];
if(sum == 0){
ArrayList<Integer> unit = new ArrayList<Integer>();
unit.add(num[i]);
unit.add(num[low]);
unit.add(num[high]);
if(!hs.contains(unit)){
hs.add(unit);
res.add(unit);
}
low++;
high--;
}else if(sum > 0)
high --;
else
low ++;
}
}
return res;
}
同时,解决duplicate问题,也可以通过挪动指针来解决判断,当找到一个合格结果时,将3个加数指针挪动到与当前值不同的地方,才再进行继续判断,代码如下:
1 public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
2 ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
3 if(num.length<3||num == null)
4 return res;
5
6 Arrays.sort(num);
7
8 for(int i = 0; i <= num.length-3; i++){
9 if(i==0||num[i]!=num[i-1]){//remove dupicate
int low = i+1;
int high = num.length-1;
while(low<high){
int sum = num[i]+num[low]+num[high];
if(sum == 0){
ArrayList<Integer> unit = new ArrayList<Integer>();
unit.add(num[i]);
unit.add(num[low]);
unit.add(num[high]);
res.add(unit);
low++;
high--;
while(low<high&&num[low]==num[low-1])//remove dupicate
low++;
while(low<high&&num[high]==num[high+1])//remove dupicate
high--;
}else if(sum > 0)
high --;
else
low ++;
}
}
}
return res;
}
3 Sum leetcode java的更多相关文章
- 【LeetCode】Path Sum ---------LeetCode java 小结
Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- Minimum Path Sum leetcode java
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
- Binary Tree Maximum Path Sum leetcode java
题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...
- Path Sum leetcode java
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- 4 Sum leetcode java
题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- Two Sum Leetcode Java
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- Combination Sum leetcode java
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- LeetCode算法题-Path Sum(Java实现)
这是悦乐书的第169次更新,第171篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第28题(顺位题号是112).给定二叉树和整数sum,确定树是否具有根到叶路径,使得沿路 ...
随机推荐
- java实现两台电脑间TCP协议文件传输
记录下之前所做的客户端向服务端发送文件的小项目,总结下学习到的一些方法与思路. 注:本文参考自<黑马程序员>视频. 首先明确需求,在同一局域网下的机器人A想给喜欢了很久的机器人B发送情书, ...
- JAVAEE——宜立方商城07:Linux上搭建Solr服务、数据库导入索引库、搜索功能的实现
1. 学习计划 1.Solr服务搭建 2.Solrj使用测试 3.把数据库中的数据导入索引库 4.搜索功能的实现 2. Solr服务搭建 2.1. Solr的环境 Solr是java开发. 需要安装j ...
- 【基础知识】ASP.NET[基础二(aspx)]
1.cs可以调用aspx中的runat=server控件,aspx中也可以访问测试中定义的字段.函数,还可以编写复杂的C#代码,for等所有C#代码都可以写在aspx中(不推荐这样写): 2.把代码写 ...
- WS快捷键
打开文件: Ctrl + Shift + N 打开类: Ctrl + N 打开函数: Ctrl + F12 “超级”打开: 双击 shift,可以 search anywhere. 复制整行: Ctr ...
- JavaSE基础之封装
JavaSE基础之封装 一.Java中的封装 1.字面意思: 包装: 2.专业含义: 面向对象的三大特征之一: 指的是将对象的状态信息隐藏在对象内部,不允许外部程序直接访问对象内部信息,而是通过该类所 ...
- MySQL主从复制(Replication)(MySQL数据同步)配置
MySQL是开源的关系型数据库系统.复制(Replication)是从一台MySQL数据库服务器(主服务器master)复制数据到另一个服务器(从服务器slave)的一个进程. 配置主服务器(mast ...
- bzoj3173 Splay 维护前缀中的最大值
大致题意: 有一个空序列,依次插入1~N到该序列中,每次指定插入的位置,每次插入完成返回当前序列的LIS的长度. 题解: 设dp[i]表示 前缀1~i的最长上升子序列的长度. 因为是按照递增顺序插入的 ...
- Codeforces Round #355 (Div. 2) B. Vanya and Food Processor 水题
B. Vanya and Food Processor 题目连接: http://www.codeforces.com/contest/677/problem/B Description Vanya ...
- Automate Screen or Button Taps via Tasker : Simulating keypress events
When using Tasker, sometimes we want to do some automation on screen e.g. screen or button taps. At ...
- HTTP的KeepAlive是开启还是关闭?
HTTP的KeepAlive是开启还是关闭? http://itindex.net/detail/50719-http-keepalive 1.KeepAlive的概念与优势 HTTP的KeepAli ...