forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_1.py
More file actions
47 lines (35 loc) · 1.38 KB
/
problem_1.py
File metadata and controls
47 lines (35 loc) · 1.38 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
class MyHashSet:
def __init__(self):
self.Bucket=1000
self.BucketItems=1000
self.Storage=[None]*self.Bucket
def hash1(self, key: int) -> None:
return key % self.Bucket
def hash2(self, key: int) -> None:
return key // self.BucketItems
def add(self, key: int) -> None: # O(1)
primary_index=self.hash1(key)
if self.Storage[primary_index] is None:
if primary_index==0:
self.Storage[primary_index]=[False]*(self.BucketItems+1)
else:
self.Storage[primary_index]=[False]*self.BucketItems
secondary_index=self.hash2(key)
self.Storage[primary_index][secondary_index]=True
def remove(self, key: int) -> None: # O(1)
primary_index=self.hash1(key)
if self.Storage[primary_index] is None:
return
secondary_index=self.hash2(key)
self.Storage[primary_index][secondary_index]= False
def contains(self, key: int) -> bool: # O(1)
primary_index=self.hash1(key)
if self.Storage[primary_index] is None:
return False
secondary_index=self.hash2(key)
return self.Storage[primary_index][secondary_index]
# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)