FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode-study/3sum/YoungSeok-Choi.java at main · DaleStudy/leetcode-study · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
DaleStudy
/
leetcode-study
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
361
Star
155
Code
Issues
75
Pull requests
5
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode-study
/
3sum
/
YoungSeok-Choi.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
65 lines (51 loc) · 1.69 KB
Breadcrumbs
leetcode-study
/
3sum
/
YoungSeok-Choi.java
Copy path
File metadata and controls
65 lines (51 loc) · 1.69 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
);
}
}
Back
|
FazBrowse Home
|
New Git URL