forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforest000014.java
More file actions
63 lines (57 loc) · 2 KB
/
forest000014.java
File metadata and controls
63 lines (57 loc) · 2 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
# Time Complexity: O(n^2)
# Space Complexity: O(1)
*/
class Solution {
public List<List<Integer>> threeSum1(int[] nums) { // solution 1
int n = nums.length;
Arrays.sort(nums);
Set<List<Integer>> ans = new HashSet<>();
for (int i = 0; i < n - 2; i++) {
for (int j = i + 1; j < n - 1; j++) {
int target = -nums[i] - nums[j]; // nums[i], nums[j]와 더해서 합이 0이 되기 위해 nums[k]가 가져야하는 값
int k = -1;
int l = j + 1;
int r = n - 1;
while (l <= r) {
int m = (r - l) / 2 + l;
if (nums[m] == target) {
k = m;
break;
} else if (nums[m] < target) {
l = m + 1;
} else {
r = m - 1;
}
}
if (k != -1) { // binary search에서 target을 찾은 경우
ans.add(new ArrayList<>(Arrays.asList(nums[i], nums[j], nums[k])));
}
}
}
return new ArrayList<>(ans);
}
public List<List<Integer>> threeSum(int[] nums) { // solution 2
int n = nums.length;
Arrays.sort(nums);
Set<List<Integer>> ans = new HashSet<>();
for (int i = 0; i < n - 2; i++) {
int l = i + 1;
int r = n - 1;
int target = -nums[i];
while (l < r) {
int sum = nums[l] + nums[r];
if (sum == target) {
ans.add(new ArrayList<>(Arrays.asList(nums[i], nums[l], nums[r])));
l++;
r--; // 또 다른 (l, r) 조합이 있을 수 있으므로, loop를 계속 이어간다.
} else if (sum < target) {
l++;
} else {
r--;
}
}
}
return new ArrayList<>(ans);
}
}