forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoungSeok-Choi.java
More file actions
65 lines (51 loc) · 1.69 KB
/
YoungSeok-Choi.java
File metadata and controls
65 lines (51 loc) · 1.69 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
64
65
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
// NOTE: 실행시간 1.5초..
// 시간 복잡도: O(n^2)
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
Set<List<Integer>> result = new HashSet<>();
for(int i = 0; i < nums.length - 2; i++) {
int startIdx = i + 1;
int endIdx = nums.length - 1;
while(startIdx < endIdx) {
int sum = nums[i] + nums[startIdx] + nums[endIdx];
if(sum == 0) {
result.add(Arrays.asList(nums[i], nums[startIdx], nums[endIdx]));
}
if(sum > 0) {
endIdx--;
} else {
startIdx++;
}
}
}
return new ArrayList<>(result);
}
}
// 309 / 314 testcases passed Time Limit Exceeded
// NOTE: 시간복잡도 O(n^3)
class WrongSolution {
public List<List<Integer>> threeSum(int[] nums) {
Set<List<Integer>> res = new HashSet<>();
List<Integer> temp = new ArrayList<>();
temp.remove(3);
for(int i = 0; i < nums.length; i++) {
for(int j = 0; j < i; j++) {
for(int k = 0; k < j; k++) {
if((nums[i] + nums[j] + nums[k]) == 0) {
List<Integer> solution = Arrays.asList(nums[i], nums[j], nums[k]);
Collections.sort(solution);
res.add(solution);
}
}
}
}
return new ArrayList<>(res);
}
}