-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdesign-hashset.py
67 lines (50 loc) · 1.67 KB
/
design-hashset.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class LinkedListNode:
def __init__(self, val: int) -> None:
self.val = val
self.next = None
class MyHashSet:
def __init__(self):
"""
Initialize your data structure here.
"""
self._hashset = [None] * 10000
def _hash(self, key: int) -> int:
return key % len(self._hashset) # hash(val) - other option
def add(self, key: int) -> None:
if self.contains(key):
return
new_node = LinkedListNode(key)
node = self._hashset[self._hash(key)]
if node:
while node.next:
node = node.next
node.next = new_node
else:
self._hashset[self._hash(key)] = new_node
def remove(self, key: int) -> None:
node = self._hashset[self._hash(key)]
if node:
prev_node, cur_node, next_node = None, node, node.next
while cur_node and cur_node.val != key:
prev_node, cur_node = cur_node, next_node
next_node = cur_node.next if cur_node else None
if prev_node:
prev_node.next = next_node
else:
self._hashset[self._hash(key)] = next_node
def contains(self, key: int) -> bool:
"""
Returns true if this set contains the specified element
"""
node = self._hashset[self._hash(key)]
if node:
while node:
if node.val == key:
return True
node = node.next
return False
# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)