Build your own hash map from integer keys to integer values.
Write the class MyHashMap:
MyHashMap() creates an empty map.put(key, value) stores value under key. If key already has a value, the new one replaces it.get(key) returns the value stored under key, or -1 if the map has no value for key.remove(key) deletes key and its value. If key is not in the map, nothing happens.The point is to build the table yourself, so don't use a built-in hash table: no dict or set in Python, unordered_map in C++, HashMap in Java, or map in Go. The judge only checks your answers, so it can't tell whether you followed this rule.
Each example lists the calls in order, then each call's arguments, then each call's result (null for the constructor, put and remove).
Example 1
Input:
["MyHashMap", "put", "put", "get", "remove", "get", "get"]
[[], [10, 3], [25, 0], [25], [10], [10], [7]]
Output: [null, null, null, 0, null, -1, -1]
Key 25 maps to 0, which is a real value. After remove(10) key 10 is gone, and key 7 was never added.
Example 2
Input:
["MyHashMap", "put", "put", "remove", "get", "put", "get"]
[[], [1000000, 9], [1000000, 4], [42], [1000000], [42, 1000000], [42]]
Output: [null, null, null, null, 4, null, 1000000]
The second put replaces 9 with 4. Removing key 42 before it exists does nothing.
Example 3
Input:
["MyHashMap", "get", "put", "remove", "remove", "get", "put", "get"]
[[], [5], [5, 6], [5], [5], [5], [5, 8], [5]]
Output: [null, -1, null, null, null, -1, null, 8]
Removing a key twice is fine, and a removed key can be added again.
Constraints
0 <= key <= 10⁶0 <= value <= 10⁶10⁴ calls in total are made to put, get and remove.Turn a key into an array index with a hash function, such as key % B for B buckets. Different keys can land on the same index, so each bucket must hold several entries.
["MyHashMap","put","put","get","remove","get","get"] [[],[10,3],[25,0],[25],[10],[10],[7]]
[null,null,null,0,null,-1,-1]