-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path705.py
More file actions
31 lines (24 loc) · 793 Bytes
/
Copy path705.py
File metadata and controls
31 lines (24 loc) · 793 Bytes
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
from collections import deque as deque
class MyHashSet:
def __init__(self):
self.hashKey = 1000
self.hashes = [ deque() for _ in range(0,self.hashKey) ]
def getHash(self, key):
return key % self.hashKey
def add(self, key: int) -> None:
if self.contains(key):
return
hashIdx = self.getHash(key)
self.hashes[hashIdx].append(key)
def remove(self, key: int) -> None:
hashIdx = self.getHash(key)
if not self.contains(key):
return
self.hashes[hashIdx].remove(key)
def contains(self, key: int) -> bool:
hashIdx = self.getHash(key)
try:
_ = self.hashes[hashIdx].index(key)
except ValueError:
return False
return True