blob: 44a70cc3a7ea88ec3dd09a534f723ac631aa82f3 (
plain)
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
|
# 706. Design HashMap
"""
design a hashmap without usin any built-in hash table libraries. implement
the 'MyHashMap' class:
- myHashMapCreate() initialises the object with an empty map
- put() inserts a key, value pair into the hashmap. if the key already exists
in the map, update the corresponding value
- get() returns the value to which the specified key is mapped, or -1 if this
map contains no mapping for the key
- remove() removes the key and its corresponding value if the map contains
the mapping for the key
"""
class MyHashMap(object):
def __init__(self):
self.data = [None] * 1000001
def put(self, key, value):
self.data[key] = value
def get(self, key):
value = self.data[key]
return value if value != None else -1
def remove(self, key):
self.data[key] = None
|