-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesign_HashMap.Py
More file actions
59 lines (39 loc) · 1.23 KB
/
Design_HashMap.Py
File metadata and controls
59 lines (39 loc) · 1.23 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
''' Problem : DESIGN HASHMAP '''
#CODE :
class MyHashMap:
def __init__(self):
"""
Initialize your data structure here.
"""
self.block = 1000
self._map = []
self.len = 0
self.incr()
def incr(self) :
self._map += [-1] * self.block
self.len += self.block
def put(self, key: int, value: int) -> None:
"""
value will always be non-negative.
"""
while key > self.len:
self.incr()
self._map[key] = value
def get(self, key: int) -> int:
"""
Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
"""
if key < self.len:
return self._map[key]
return -1
def remove(self, key: int) -> None:
"""
Removes the mapping of the specified value key if this map contains a mapping for the key
"""
if key < self.len:
self._map[key] = -1
# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key,value)
# param_2 = obj.get(key)
# obj.remove(key)