1+ """
2+ Design a HashMap without using any built-in hash table libraries.
3+
4+ To be specific, your design should include these functions:
5+
6+ put(key, value) : Insert a (key, value) pair into the HashMap. If the value already exists in the HashMap, update the value.
7+ get(key): Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
8+ remove(key) : Remove the mapping for the value key if this map contains the mapping for the key.
9+
10+ Example:
11+
12+ MyHashMap hashMap = new MyHashMap();
13+ hashMap.put(1, 1);
14+ hashMap.put(2, 2);
15+ hashMap.get(1); // returns 1
16+ hashMap.get(3); // returns -1 (not found)
17+ hashMap.put(2, 1); // update the existing value
18+ hashMap.get(2); // returns 1
19+ hashMap.remove(2); // remove the mapping for 2
20+ hashMap.get(2); // returns -1 (not found)
21+
22+ Note:
23+
24+ All keys and values will be in the range of [0, 1000000].
25+ The number of operations will be in the range of [1, 10000].
26+ Please do not use the built-in HashMap library.
27+ """
28+
29+
30+ class MyHashMap :
31+
32+ def __init__ (self ):
33+ """
34+ Initialize your data structure here.
35+ """
36+ self .hmap = [- 1 ] * 1000001
37+
38+ def put (self , key : int , value : int ) -> None :
39+ """
40+ value will always be non-negative.
41+ """
42+ self .hmap [key ] = value
43+
44+ def get (self , key : int ) -> int :
45+ """
46+ Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
47+ """
48+ return self .hmap [key ]
49+
50+ def remove (self , key : int ) -> None :
51+ """
52+ Removes the mapping of the specified value key if this map contains a mapping for the key
53+ """
54+ self .hmap [key ] = - 1
55+
56+ # Your MyHashMap object will be instantiated and called as such:
57+ # obj = MyHashMap()
58+ # obj.put(key,value)
59+ # param_2 = obj.get(key)
60+ # obj.remove(key)
0 commit comments