-
Notifications
You must be signed in to change notification settings - Fork 5
/
15. 3Sum.java
47 lines (47 loc) · 1.91 KB
/
15. 3Sum.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
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
return optimized(nums);
}
private List<List<Integer>> optimized(int[] nums) {
// TC: O(NLogN+N**2) ; SC: O(M)
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
int target = -1 * nums[i];
int start = i+1;
int end = nums.length-1;
while (start < end) {
int sm = nums[start] + nums[end];
if (sm == target) {
result.add( Arrays.asList(nums[i], nums[start], nums[end]) );
while (start+1 < nums.length && nums[start+1] == nums[start]) start++;
while (end-1 > 0 && nums[end-1] == nums[end]) end--;
start++;
end--;
}
else if (sm > target) {
end--;
} else {
start++;
}
}
while (i+1 < nums.length && nums[i+1] == nums[i]) i++;
}
return result;
}
private List<List<Integer>> bruteforce(int[] nums) {
// TC: O(N**3) ; SC: O(N)
Set<List<Integer>> result = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
for (int j = i+1; j < nums.length; j++) {
for (int k = j+1; k < nums.length; k++) {
if (nums[i] + nums[j] + nums[k] == 0) {
result.add(sorted(nums[i], nums[j], nums[k] ));
}
}
}
}
return result.stream().toList();
}