-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
63 lines (56 loc) · 1.97 KB
/
Solution.java
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* User: Changle
* Date: 2018-03-11 10:21
* Source: https://leetcode.com/problems/4sum/description/
*/
/**
* 排序,然后确定两个数,然后使用对撞指针移动另外两个数
*
* 时间复杂度: O(n^3)
* 空间复杂度: O(n)
*/
public class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
for (int j = i + 1; j < nums.length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
int left = j + 1;
int right = nums.length - 1;
while (left < right) {
if (nums[i] + nums[j] + nums[left] + nums[right] < target) {
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
left++;
} else if (nums[i] + nums[j] + nums[left] + nums[right] > target) {
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
right--;
} else {
res.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
left++;
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
right--;
}
}
}
}
return res;
}
}