Skip to content

Commit 31290f2

Browse files
committed
feat(leetcode/217): Solve Contains Duplicate (Easy) - feedback map to set
1 parent b8140d5 commit 31290f2

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

contains-duplicate/renovizee.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
11
import java.util.HashMap;
2+
import java.util.HashSet;
23
import java.util.Map;
4+
import java.util.Set;
35

46
// tag renovizee 1week
57
// https://github.com/DaleStudy/leetcode-study/issues/217
68
// https://leetcode.com/problems/contains-duplicate/
79
class Solution {
10+
11+
// Solv2: hash set (feedback)
812
public boolean containsDuplicate(int[] nums) {
913
// 시간복잡도 : O(n)
1014
// 공간복잡도 : O(n)
11-
Map<Integer,Integer> countMap = new HashMap<>();
15+
Set<Integer> numsSet = new HashSet<>();
1216
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)) {
1718
return true;
1819
}
20+
numsSet.add(num);
1921
}
2022
return false;
2123
}
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+
// }
2240
}
2341

2442
//-------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)