Skip to content

Commit afd0af8

Browse files
donghyeon95donghyeon95
authored andcommitted
add solution: Contains Duplicate DaleStudy#217
1 parent 8901b1d commit afd0af8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public boolean containsDuplicate(int[] nums) {
5+
/*
6+
* -- ํ’€์ด --
7+
* nums๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ HashSet์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์„œ ์ค‘๋ณต๋˜์—ˆ๋Š” ์ง€ ํ™•์ธํ•œ๋‹ค.
8+
*
9+
* -- ์‹œ๊ฐ„ ๋ณต์žก๋„ --
10+
* ๊ธธ์ด N์ธ nums๋ฅผ ์ˆœํ™˜ํ•˜๋Š”๋ฐ ๋Œ€ํ•œ ์‹œ๊ฐ„ ๋ณต์žก๋„ => O(N)
11+
* hashSet์˜ add์— ๋Œ€ํ•œ ์‹œ๊ฐ„ ๋ณต์žก๋„ => O(1)
12+
* ์ „์ฒด ์‹œ๊ฐ„ ๋ณต์žก๋„ O(1)*O(N) =O(n)
13+
* ------------------------------------------
14+
*
15+
* -- ๊ณต๊ฐ„ ๋ณต์žก๋„ --
16+
* ๊ธธ์ด N์ธ nums๋ฅผ ๋„ฃ์„ Hashset์ด ์žˆ์–ด์•ผ ํ•˜๊ธฐ์— O(N)
17+
* -------------------------------------------
18+
*/
19+
20+
// ์ค‘๋ณต์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” set ์„ ์–ธ
21+
HashSet<Integer> hashSet = new HashSet<>();
22+
23+
for (int num: nums) {
24+
// set์— ์žˆ๋‹ค๋ฉด
25+
if (!hashSet.add(num)) return true;
26+
}
27+
28+
return false;
29+
}
30+
}

0 commit comments

Comments
ย (0)