forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem1.py
More file actions
49 lines (39 loc) · 1.53 KB
/
problem1.py
File metadata and controls
49 lines (39 loc) · 1.53 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
# Design-1
## Problem 1:(https://leetcode.com/problems/design-hashset/)
class MyHashSet:
def __init__(self):
self.primary_size = 1000
self.secondary_size = 1000
self.storage = [None]*1000
def getPrimaryHash(self,val):
return val%self.primary_size
def getSecondaryHash(self,val):
return val // self.secondary_size
def add(self, key: int) -> None:
# add only if it doesnt exist
primary_hash = self.getPrimaryHash(key)
secondary_hash = self.getSecondaryHash(key)
if self.storage[primary_hash] == None:
if primary_hash == 0:
self.storage[primary_hash] = [False]* (self.secondary_size+1)
else:
self.storage[primary_hash] = [False]*self.secondary_size
self.storage[primary_hash][secondary_hash] = True
def remove(self, key: int) -> None:
primary_hash = self.getPrimaryHash(key)
secondary_hash = self.getSecondaryHash(key)
if self.storage[primary_hash] == None:
return None
else:
self.storage[primary_hash][secondary_hash] = False
def contains(self, key: int) -> bool:
primary_hash = self.getPrimaryHash(key)
secondary_hash = self.getSecondaryHash(key)
if self.storage[primary_hash] == None:
return False
return self.storage[primary_hash][secondary_hash]
# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)