forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_2.py
More file actions
53 lines (44 loc) · 1.3 KB
/
Problem_2.py
File metadata and controls
53 lines (44 loc) · 1.3 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Time Complexity : O(1)
# Space Complexity : O(n) -> Space will be allocated for every given key.
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this :No
class MyHashSet(object):
def __init__(self):
self.containers= [[]]*1000
def add(self, key):
"""
:type key: int
:rtype: None
"""
cont_idx= key % 1000
if key not in self.containers[cont_idx]:
self.containers[cont_idx].append(key)
def remove(self, key):
"""
:type key: int
:rtype: None
"""
cont_idx= key % 1000
if key in self.containers[cont_idx]:
self.containers[cont_idx].remove(key)
def contains(self, key):
"""
:type key: int
:rtype: bool
"""
cont_idx= key%1000
if key in self.containers[cont_idx]:
return True
return False
# Your MyHashSet object will be instantiated and called as such:
myHashSet = MyHashSet()
myHashSet.add(411)
myHashSet.add(267)
myHashSet.add(23)
myHashSet.add(76)
print(myHashSet.contains(411))
print(myHashSet.contains(76))
myHashSet.add(212)
print(myHashSet.contains(212))
myHashSet.remove(212)
print(myHashSet.contains(212))