Skip to content

Commit 00edbae

Browse files
Merge pull request #1 from huanleofficial/huanleofficial-patch-1
Create 0705-design-hashset
2 parents 1f4cfb0 + dcd0fd9 commit 00edbae

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

python/0705-design-hashset

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class MyHashSet:
2+
3+
def __init__(self):
4+
# Initializing table and size
5+
self.size = 10000
6+
self.table = [None] * self.size
7+
8+
def add(self, key: int) -> None:
9+
# Get bucket
10+
hk = key % self.size
11+
12+
# Check and append if value is not in table with bucket
13+
if self.table[hk] == None:
14+
self.table[hk] = [key]
15+
else:
16+
self.table[hk].append(key)
17+
18+
def remove(self, key: int) -> None:
19+
# Get bucket
20+
hk = key % self.size
21+
22+
# Check and remove if value is in table with bucket
23+
if self.table[hk] != None:
24+
while key in self.table[hk]:
25+
self.table[hk].remove(key)
26+
27+
def contains(self, key: int) -> bool:
28+
# Get bucket
29+
hk = key % self.size
30+
31+
# Check for value in table with bucket
32+
if self.table[hk] != None:
33+
return key in self.table[hk]
34+
return False
35+
36+
# Your MyHashSet object will be instantiated and called as such:
37+
# obj = MyHashSet()
38+
# obj.add(key)
39+
# obj.remove(key)
40+
# param_3 = obj.contains(key)

0 commit comments

Comments
 (0)