forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignHashset.py
More file actions
26 lines (15 loc) · 1.1 KB
/
DesignHashset.py
File metadata and controls
26 lines (15 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#This MyHashSet class uses a defaultdict from the collections module to simulate a hash set. In the __init__ method, an empty defaultdict named mp is initialized to store keys, defaulting values to integers. The add method sets the value for a key to True, marking it as present in the set. The remove method sets the value to False, indicating removal. The contains method returns the boolean state of the key, signifying its presence. This implementation ensures average O(1) time complexity for add, remove, and contains operations. The space complexity is O(n), where n is the number of unique keys added, since each unique key requires space in the dictionary.
class MyHashSet:
def __init__(self):
self.mp=defaultdict(int)
def add(self, key: int) -> None:
self.mp[key]=True
def remove(self, key: int) -> None:
self.mp[key]=False
def contains(self, key: int) -> bool:
return self.mp[key]
# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)