-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFourSum.java
More file actions
45 lines (42 loc) · 1.45 KB
/
FourSum.java
File metadata and controls
45 lines (42 loc) · 1.45 KB
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
package problem011_020;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
/**
* 018. Four Sum
* 双指针做法,三层循环,时间复杂度为O(n^3)
*/
public class FourSum {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
HashSet<List<Integer>> set = new HashSet<>(); // 设置hashset用来去重
Arrays.sort(nums);
for (int i = 0; i < nums.length - 3; i++) {
for (int j = i+1; j < nums.length - 2; j++) {
int lo = j + 1, hi = nums.length - 1;
while (lo < hi) {
int sum = nums[i] + nums[j] + nums[lo] + nums[hi];
if (sum == target) {
List<Integer> temp = new ArrayList<>();
temp.add(nums[i]);
temp.add(nums[j]);
temp.add(nums[lo]);
temp.add(nums[hi]);
if (!set.contains(temp)) {
set.add(temp);
result.add(temp);
}
lo ++;
hi --;
} else if (sum < target) {
lo ++;
} else {
hi --;
}
}
}
}
return result;
}
}