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 11import java .util .HashMap ;
2+ import java .util .HashSet ;
23import 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/
79class 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//-------------------------------------------------------------------------------------------------------------
You can’t perform that action at this time.
0 commit comments