Four Sum || LeetCode-18

Problem linkhttps://leetcode.com/problems/4sum/#/description

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] ]

<问题描述>

给定一个整形数组,需要从数组中找出四个整数,使得它们的和等于另外一个给定的整数

你可以假定每一个输入有且仅有一个答案,并且不能使用同一个元素两次

给出的fourSum函数应该返回找到的四个整数值

输入:numbers = [1, 0, -1, 0, -2, 2], target = 0 (注意:输入的数组不一定有序)

输出:[[-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2]]

Approach #1 (HashSet)

Analysis

可以延用之前处理Two Sum问题的方法,先是两层遍历,固定其中两个数nums[i]和nums[j],然后利用low和high两个指针相互移动,找到和为target的四个整数,如果利用hashset中value的唯一性,判断其是否在hashset中出现过,若没有出现过则加入,low和high继续移动。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public List<List<integer>> fourSum(int[] nums, int target){
List<List<integer>> result = new ArrayList<List<integer>>();
HashSet<List<integer>> set = new HashSet<List<integer>>();
Arrays.sort(nums); // 数组排序
for (int i = 0; i < nums.length - 3; i++) {
for (int j = i + 1; j < nums.length - 2; j++) {
int low = j + 1;
int high = nums.length - 1;
while(low < high) {
int sum = nums[i] + nums[j] + nums[low] + nums[high];
if (sum == target) {
List<integer> list = new ArrayList<integer>();
list.add(nums[i]);
list.add(nums[j]);
list.add(nums[low]);
list.add(nums[high]);
if (!set.contains(list)) { // 利用hashset处理重复元素
set.add(list);
result.add(list);
}
low++; // 结果不唯一,low和high指针应该继续移动
high--;
} else if (sum > target) {
high--;
} else
low++;
}
}
}
return result;
}

Complexity

时间复杂度:O(n^2)

空间复杂度:O(1)O(n)

Approach #2 (Sorting With Two Pointers)

Analysis

方法和Two Sum问题的差不多,只不过现在是查找四个元素。相比hashset方法,只是没有hashset的去重,需要独立处理nums[i]、nums[j]、nums[low]、nums[high]元素的重复问题。去除重复元素的关键就是,i,j,low,high首次碰到重复元素时允许遍历。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public List<List<integer>> fourSum(int[] nums, int target){
List<List<integer>> result = new ArrayList<List<integer>>();
Arrays.sort(nums);
for (int i = 0; i < nums.length - 3; i++) {
if(i > 0 && nums[i] == nums[i - 1]) // 处理nums[i]的重复
continue;
for (int j = i + 1; j < nums.length - 2; j++) {
if(j > i + 1 && nums[j] == nums[j - 1]) // 处理nums[j]的重复
continue;
int low = j + 1;
int high = nums.length - 1;
while(low < high){
int sum = nums[i] + nums[j] + nums[low] + nums[high];
if (sum == target) {
List<integer> list = new ArrayList<integer>();
list.add(nums[i]);
list.add(nums[j]);
list.add(nums[low]);
list.add(nums[high]);
result.add(list);
low++;
high--;
while (low < high && nums[low] == nums[low - 1]) { // 处理nums[low]的重复
low++;
}
while (low < high && nums[high] == nums[high + 1]) { // 处理nums[high]的重复
high--;
}
} else if (sum > target) {
high--;
} else
low++;
}
}
}
return result;
}

Complexity

时间复杂度:O(n^3)

空间复杂度:O(1)取决于排序算法

Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2019 kevinyangI All Rights Reserved.

UV : | PV :