forked from super30admin/Design-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass MyHashSet:.py
45 lines (35 loc) · 1.29 KB
/
class MyHashSet:.py
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
class MyHashSet:
def __init__(self):
self.buckets = 1000
self.bucketItems = 1000
self.storage = [None] * self.buckets
def getBucket(self, key):
return key % self.buckets
def getBucketItem(self, key):
return key // self.bucketItems
def add(self, key: int) -> None:
bucket = self.getBucket(key)
bucketItem = self.getBucketItem(key)
if not self.storage[bucket]:
if bucket == 0:
self.storage[bucket] = [False] * (self.bucketItems + 1)
else:
self.storage[bucket] = [False] * self.bucketItems
self.storage[bucket][bucketItem] = True
def remove(self, key: int) -> None:
bucket = self.getBucket(key)
bucketItem = self.getBucketItem(key)
if not self.storage[bucket]:
return
self.storage[bucket][bucketItem] = False
def contains(self, key: int) -> bool:
bucket = self.getBucket(key)
bucketItem = self.getBucketItem(key)
if not self.storage[bucket]:
return False
return self.storage[bucket][bucketItem]
# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)