File tree Expand file tree Collapse file tree 1 file changed +23
-5
lines changed Expand file tree Collapse file tree 1 file changed +23
-5
lines changed Original file line number Diff line number Diff line change 1
1
import java .util .HashMap ;
2
+ import java .util .HashSet ;
2
3
import java .util .Map ;
4
+ import java .util .Set ;
3
5
4
6
// tag renovizee 1week
5
7
// https://github.com/DaleStudy/leetcode-study/issues/217
6
8
// https://leetcode.com/problems/contains-duplicate/
7
9
class Solution {
10
+
11
+ // Solv2: hash set (feedback)
8
12
public boolean containsDuplicate (int [] nums ) {
9
13
// 시간복잡도 : O(n)
10
14
// 공간복잡도 : O(n)
11
- Map <Integer , Integer > countMap = new HashMap <>();
15
+ Set <Integer > numsSet = new HashSet <>();
12
16
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
+ if (numsSet .contains (num )) {
17
18
return true ;
18
19
}
20
+ numsSet .add (num );
19
21
}
20
22
return false ;
21
23
}
24
+
25
+ // Solv1: hash map
26
+ // public boolean containsDuplicate(int[] nums) {
27
+ // // 시간복잡도 : O(n)
28
+ // // 공간복잡도 : O(n)
29
+ // Map<Integer,Integer> countMap = new HashMap<>();
30
+ // for (int num : nums) {
31
+ // int count = countMap.getOrDefault(num, 0);
32
+ // int addCount = count + 1;
33
+ // countMap.put(num, addCount);
34
+ // if (addCount == 2) {
35
+ // return true;
36
+ // }
37
+ // }
38
+ // return false;
39
+ // }
22
40
}
23
41
24
42
//-------------------------------------------------------------------------------------------------------------
You can’t perform that action at this time.
0 commit comments