[LeetCode] 18. 4Sum ☆☆
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: 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]
]
解法1:
在 3Sum 的基础上再加一层循环即可,3Sum 时间复杂度为 O(n2),所以这个方法时间复杂度为 O(n3)。
public class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length < 4) {
return res;
} Arrays.sort(nums);
for (int i = 0; i < nums.length - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
} int sum3 = target - nums[i]; // 后3个数之和需等于sum3
for (int j = i + 1; j < nums.length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
} int sum2 = sum3 - nums[j]; // 后2个数之和需等于sum3
int left = j + 1, right = nums.length - 1; while (left < right) {
if (nums[left] + nums[right] == sum2) {
List<Integer> quad = new ArrayList<>();
quad.add(nums[i]);
quad.add(nums[j]);
quad.add(nums[left]);
quad.add(nums[right]);
res.add(quad); while (left < right && nums[left++] == nums[left]) {}
while (left < right && nums[right--] == nums[right]) {} } else if (nums[left] + nums[right] < sum2) {
while (left < right && nums[left++] == nums[left]) {} } else {
while (left < right && nums[right--] == nums[right]) {}
}
}
}
}
return res;
}
}
解法2:
先排序 (O(nlogn)),然后遍历整个数组,以每两个数的和作为key将两个数的index存于HashMap中,由于存在和相同的情况,因此HashMap中的每个键对应的是一个List,每个List中保存着多组index对,这一操作时间复杂度为 (O(n2))。
遍历每个key,计算出需要满足4Sum为target的另外一个2Sum,两个2Sum对应的List分别为listA和listB,遍历其中的每一组index,分别为[a0, a1] 和 [b0, b1]。为避免找到index重复的结果,只寻找满足 a0 < b0, a0 < b1, a1 < b0, a1 < b1 的结果,而因为本身就有 a0 < a1, b0 < b1,所以只需满足 a1 < b0 即可。判断找到的结果是否已存在,不存在再把结果加到res中。这一操作比较复杂,时间复杂度不好算,据说是(O(n2logn)),但LeetCode给出的结果是超时。。。。
public class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length < 4) {
return res;
} Arrays.sort(nums); HashMap<Integer, List<Integer[]>> map = new HashMap<>();
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
int sum = nums[i] + nums[j];
Integer[] pair = {i, j}; if (!map.containsKey(sum)) {
map.put(sum, new ArrayList<Integer[]>());
}
map.get(sum).add(pair);
}
} Set<Integer> keys = map.keySet();
for (int key : keys) {
List<Integer[]> listA = map.get(key);
List<Integer[]> listB = map.get(target - key); if (listA != null && listB != null) {
for (Integer[] pairA : listA) {
int a0 = pairA[0], a1 = pairA[1]; for (Integer[] pairB : listB) {
int b0 = pairB[0], b1 = pairB[1];
if (a1 < b0) { // 因为肯定存在: a0 < a1, b0 < b1
List<Integer> list = new ArrayList<>();
list.add(nums[a0]);
list.add(nums[a1]);
list.add(nums[b0]);
list.add(nums[b1]);
if (!res.contains(list)) res.add(list);
}
}
}
}
} return res;
}
}
[LeetCode] 18. 4Sum ☆☆的更多相关文章
- [LeetCode] 18. 4Sum 四数之和
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- LeetCode——18. 4Sum
一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...
- LeetCode 18 4Sum (4个数字之和等于target)
题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...
- LeetCode 18. 4Sum (四数之和)
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- leetcode 15 3sum & leetcode 18 4sum
3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...
- Leetcode 18. 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- Java [leetcode 18]4Sum
问题描述: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- C#解leetcode 18. 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- [leetcode]18. 4Sum四数之和
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
随机推荐
- 一些容易记混的c++相关知识点
一些容易记混的c++相关知识. 截图自:<王道程序员面试宝典>
- css3美化radio样式
.magic-radio{ position: absolute; display: none; } .magic-radio + label { position: relative; displa ...
- TCP 的有限状态机
TCP 有限状态机的图中每一个方框都是 TCP 可能具有的状态. 每个方框中的大写英文字符串是 TCP 标准所使用的 TCP 连接状态名. 状态之间的箭头表示可能发生的状态变迁. 箭头旁边的字,表明引 ...
- Git 应用补丁报错 “sha1 information is lacking or useless”
因为现场代码在客户局域网内,不能连接到公司网络,所以一般更新的时候都是打补丁, 然后在客户现场应用补丁,但是最近在应用补丁的时候出现了如下问题: ... fatal: sha1 information ...
- bzoj3998-弦论
给定一个长度为\(n(n\le 5\times 10^5)\)的字符串,求它的第\(k\)小字串.有两种模式: \(Type=0\),不同位置的相同字串只算一个 \(Type=1\),不同位置相同字串 ...
- Python os模块常用函数详解
当前使用平台: os.name #返回当前使用平台的代表字符,Windows用'nt'表示,Linux用'posix'表示 当前路径和文件 os.getcwd() #返回当前工作目录 os.listd ...
- 转:Simple Introduction to Dirichlet Process
来源:http://hi.baidu.com/vyfrcemnsnbgxyd/item/2f10ecc3fc35597dced4f88b Dirichlet Process(DP)是一个很重要的统计模 ...
- 【刷题】BZOJ 2038 [2009国家集训队]小Z的袜子(hose)
Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命-- 具体来说,小Z把这N只 ...
- [HDU4532]湫秋系列故事——安排座位
题面在这里 description 有\(n\)种颜色的小球,每种颜色的小球有\(a_i\)个: 要把它们摆成一排,求相邻小球颜色不相同的摆放方案数. 任意两个合理的安排方法,只要有一个位置的同学不同 ...
- [洛谷P3627][APIO2009]抢掠计划
题目大意:给你一张$n(n\leqslant5\times10^5)$个点$m(m\leqslant5\times10^5)$条边的有向图,有点权,给你起点和一些可能的终点.问从起点开始,到任意一个终 ...