3 Sum leetcode java
来源:互联网 发布:mac os x怎么更新 编辑:程序博客网 时间:2024/05/15 01:38
题目:
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)实现代码为:
public ArrayList<ArrayList<Integer>> threeSum(int[] num) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(num.length<3||num == null) return res; HashSet<ArrayList<Integer>> hs = new HashSet<ArrayList<Integer>>(); Arrays.sort(num); 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个加数指针挪动到与当前值不同的地方,才再进行继续判断,代码如下:public ArrayList<ArrayList<Integer>> threeSum(int[] num) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(num.length<3||num == null) return res; Arrays.sort(num); for(int i = 0; i <= num.length-3; i++){ 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; }Refrence:http://www.cnblogs.com/springfor/p/3859670.html
阅读全文
0 0
- [Leetcode] 3Sum (Java)
- [LeetCode][Java] 3Sum
- LeetCode : 3Sum [java]
- Java-LeetCode-3Sum
- 3Sum java leetcode
- leetcode 3Sum(Java)
- 3 Sum leetcode java
- [LeetCode Java] 15 3 Sum
- [LeetCode][Java] 3Sum Closest
- [leetcode-15]3Sum(java)
- (java)leetcode 3sum cloest
- LeetCode 3Sum 求和 java
- LeetCode : 3Sum Closest [java]
- (java)Leetcode-15. 3Sum
- leetcode-Java-15. 3Sum
- 3Sum Leetcode Python Java
- [LeetCode-Java]15. 3Sum
- [LeetCode] 15. 3Sum java
- Java中的equals和==,字符串常量池介绍
- datastage因ftp作业超时报错的解决
- 决战
- 空结构体sizeof()获取的大小是多少
- 【Java进阶-Java动态代理与AOP】01 代理类的作用与原理及AOP概念
- 3 Sum leetcode java
- 转载
- Google 的开源技术protobuf 简介与例子
- (转) hash 函数及其重要性
- 树的遍历 ALDS1_7_C: Tree Walk
- MMP的一天
- Java API(Math类和Random类 包装类 Date类、Calendar类和DateFormat类)
- 系统架构-复杂系统的产品设计与开发
- Mysql索引的优化分析-索引优化(1)