Skip to content

Commit f56624e

Browse files
authored
Create design_hash_map.py
1 parent 80b386f commit f56624e

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

design_hash_map.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
Design a HashMap without using any built-in hash table libraries.
3+
4+
Implement the MyHashMap class:
5+
6+
MyHashMap() initializes the object with an empty map.
7+
void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.
8+
int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
9+
void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.
10+
'''
11+
12+
13+
class MyHashMap:
14+
15+
def __init__(self):
16+
self.m = dict()
17+
18+
def put(self, key: int, value: int) -> None:
19+
if key in self.m.keys():
20+
self.m[key] = value
21+
else:
22+
self.m[key] = value
23+
24+
def get(self, key: int) -> int:
25+
if key in self.m.keys():
26+
27+
return self.m[key]
28+
else:
29+
return -1
30+
31+
32+
def remove(self, key: int) -> None:
33+
if key in self.m.keys():
34+
self.m.pop(key)
35+
36+
37+
# Your MyHashMap object will be instantiated and called as such:
38+
# obj = MyHashMap()
39+
# obj.put(key,value)
40+
# param_2 = obj.get(key)
41+
# obj.remove(key)

0 commit comments

Comments
 (0)