-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathDesignHashMap.py
More file actions
90 lines (71 loc) · 2.7 KB
/
DesignHashMap.py
File metadata and controls
90 lines (71 loc) · 2.7 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'''
Here I implemented my Hashmap using linear chaining for handling collisions, using a hash function.
put():
- with the index returned from hash function we go to the index and check if the index already has a chain initialized.
- if not we add a dummy node before creating the head node(because when the head had to be removed from the storage array,
it will break the connection to rest of the elements)
- if it already has values then we connect the new values to existing node.
get():
we directly fetch the value from the key
remove():
from the find we get the prevois node and delete the prev.next and connect prev.next to prev.next.next
'''
class MyHashMap:
class Node:
def __init__(self, key, val):
self.key = key
self.val = val
self.next = None
def __init__(self):
self.size = 10000
self.storage = [None] * self.size
# hash function for equal distribution
def hashFunc(self, key: int)-> int:
return key % self.size
# function to find the prev node
def find(self, head, key):
prev = None
curr = head
while curr is not None and curr.key != key:
prev = curr
curr = curr.next
return prev
def put(self, key: int, value: int) -> None:
idx = self.hashFunc(key)
# to check if the index has any chain, if not create a dummy pair
if self.storage[idx] is None:
self.storage[idx] = self.Node(-1,-1)
# to find the previous node of the target key
prev = self.find(self.storage[idx], key)
# if the index has values append with new values
if prev.next:
prev.next.val = value
else:
prev.next = self.Node(key, value)
def get(self, key: int) -> int:
idx = self.hashFunc(key)
# if no linked list exists at the index
if self.storage[idx] is None:
return -1
# to find the previous node of the target key
prev = self.find(self.storage[idx], key)
# if exists return the value
if prev.next:
return prev.next.val
# if the key is not found return -1
return -1
def remove(self, key: int) -> None:
idx = self.hashFunc(key)
# to check if it has no elements in that if not return
if self.storage[idx] is None:
return
# to find the previous node of the target key
prev = self.find(self.storage[idx], key)
if prev.next is None:
return
# to link previous node to next node of removed node
prev.next = prev.next.next
'''
Time Complexity: O(1)
Space Complexity: O(1)
'''