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 = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
- Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
- The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
A solution set is:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)
题解:
4 sum跟3 sum是一样的思路,只不过需要多考虑一个加数,这样时间复杂度变为O(n3)。
使用HashSet来解决重复问题的代码如下:
1 public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
2 HashSet<ArrayList<Integer>> hashSet = new HashSet<ArrayList<Integer>>();
3 ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
4 Arrays.sort(num);
5 for (int i = 0; i <= num.length-4; i++) {
6 for (int j = i + 1; j <= num.length-3; j++) {
7 int low = j + 1;
8 int high = num.length - 1;
9
while (low < high) {
int sum = num[i] + num[j] + num[low] + num[high];
if (sum > target) {
high--;
} else if (sum < target) {
low++;
} else if (sum == target) {
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(num[i]);
temp.add(num[j]);
temp.add(num[low]);
temp.add(num[high]);
if (!hashSet.contains(temp)) {
hashSet.add(temp);
result.add(temp);
}
low++;
high--;
}
}
}
}
return result;
}
使用挪动指针的方法来解决重复的代码如下:
1 public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
2 HashSet<ArrayList<Integer>> hashSet = new HashSet<ArrayList<Integer>>();
3 ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
4 Arrays.sort(num);
5 for (int i = 0; i <= num.length-4; i++) {
6 if(i==0||num[i]!=num[i-1]){
7 for (int j = i + 1; j <= num.length-3; j++) {
8 if(j==i+1||num[j]!=num[j-1]){
9 int low = j + 1;
int high = num.length - 1;
while (low < high) {
int sum = num[i] + num[j] + num[low] + num[high];
if (sum > target) {
high--;
} else if (sum < target) {
low++;
} else if (sum == target) {
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(num[i]);
temp.add(num[j]);
temp.add(num[low]);
temp.add(num[high]);
if (!hashSet.contains(temp)) {
hashSet.add(temp);
result.add(temp);
}
low++;
high--;
while(low<high&&num[low]==num[low-1])//remove dupicate
low++;
while(low<high&&num[high]==num[high+1])//remove dupicate
high--;
}
}
}
}
}
}
return result;
}
4 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 ...
- 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 al ...
- 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,确定树是否具有根到叶路径,使得沿路 ...
随机推荐
- 安卓webview断网处理
需求:webview在加载的时候如果网络断开,会显示默认的错误界面,长得很丑,需要单独写一个页面,在网路出错的时候显示,点击重试以后重新加载网页 乍看挺简单的需求,但在实际过程中页碰到了不少坑,主要是 ...
- Python之路【第十篇】: python基础之socket编程
阅读目录 一 客户端/服务器架构 二 osi七层 三 socket层 四 socket是什么 五 套接字发展史及分类 六 套接字工作流程 七 基于TCP的套接字 八 基于UDP的套接字 九 recv与 ...
- 选择 React Native 的理由
转载:选择 React Native 的理由 从开始知道 React Native 到现在已经过了5个月,真实的试用也经历了三个月的时间.阅读文档开始,了解是什么,到简单的理解为什么,都是在聆听不同的 ...
- bzoj5102 [POI2018]Prawnicy 线段树
$bzoj$跑的太慢了...... 我们考虑用线段树来解决这个问题 考虑扫描线 当扫到左端点$i$时,我们把线段$i$加入线段树 同时,对于每个左端点$i$,我们在线段树上二分出最远的$r$满足$r$ ...
- BZOJ.2339.[HNOI2011]卡农(思路 DP 组合 容斥)
题目链接 \(Description\) 有\(n\)个数,用其中的某些数构成集合,求构造出\(m\)个互不相同且非空的集合(\(m\)个集合无序),并满足每个数总共出现的次数为偶数的方案数. \(S ...
- hdu 1698 线段树 成段更新
题意:一段钩子,每个钩子的值为1,有若干更新,每次跟新某段的值,若干查询某段的和 基础题了 #include<cstdio> #include<iostream> #inclu ...
- java的反射机制(第三篇)
本文转载自:http://c.biancheng.net/cpp/html/1782.html Person p=new Person();这是什么?当然是实例化一个对象了.可是这种实例化对象的方法存 ...
- bzoj 4176: Lucas的数论 -- 杜教筛,莫比乌斯反演
4176: Lucas的数论 Time Limit: 30 Sec Memory Limit: 256 MB Description 去年的Lucas非常喜欢数论题,但是一年以后的Lucas却不那么 ...
- 【BZOJ-1194】潘多拉的盒子 拓扑排序 + DP
1194: [HNOI2006]潘多拉的盒子 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 456 Solved: 215[Submit][Stat ...
- HDU 5714 拍照 前缀和
拍照 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5714 Description 小明在旅游的路上看到了一条美丽的河,河上有许多船只,有的船只向左 ...