File tree Expand file tree Collapse file tree 1 file changed +20
-2
lines changed Expand file tree Collapse file tree 1 file changed +20
-2
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .HashMap ;
2
+ import java .util .Map ;
1
3
4
+ // tag renovizee 1week
2
5
// https://github.com/DaleStudy/leetcode-study/issues/217
3
6
// https://leetcode.com/problems/contains-duplicate/
4
7
class Solution {
5
8
public boolean containsDuplicate (int [] nums ) {
6
-
7
- return true ;
9
+ // 시간복잡도 : O(n)
10
+ // 공간복잡도 : O(n)
11
+ Map <Integer ,Integer > countMap = new HashMap <>();
12
+ for (int num : nums ) {
13
+ int count = countMap .getOrDefault (num , 0 );
14
+ int addCount = count + 1 ;
15
+ countMap .put (num , addCount );
16
+ if (addCount == 2 ) {
17
+ return true ;
18
+ }
19
+ }
20
+ return false ;
8
21
}
9
22
}
23
+
24
+ //-------------------------------------------------------------------------------------------------------------
25
+ // 기본 문법 피드백
26
+ // 1) Map 기본 문법, ~.getOrDefault()
27
+ //-------------------------------------------------------------------------------------------------------------
You can’t perform that action at this time.
0 commit comments