|
| 1 | +--- |
| 2 | +layout: leetcode |
| 3 | +title: "706. Design HashMap" |
| 4 | +categories: [leetcode] |
| 5 | +--- |
| 6 | + |
| 7 | +[Leetcode Link](https://leetcode.com/problems/design-hashmap/) |
| 8 | + |
| 9 | +Design a HashMap without using any built-in hash table libraries. |
| 10 | + |
| 11 | +Implement the MyHashMap class: |
| 12 | + |
| 13 | +* MyHashMap() initializes the object with an empty map. |
| 14 | +* 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. |
| 15 | +* 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. |
| 16 | +* void remove(key) removes the key and its corresponding value if the map contains the mapping for the key. |
| 17 | + |
| 18 | +``` |
| 19 | +Example 1: |
| 20 | +
|
| 21 | +Input |
| 22 | +["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"] |
| 23 | +[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]] |
| 24 | +Output |
| 25 | +[null, null, null, 1, -1, null, 1, null, -1] |
| 26 | +
|
| 27 | +Explanation |
| 28 | +MyHashMap myHashMap = new MyHashMap(); |
| 29 | +myHashMap.put(1, 1); // The map is now [[1,1]] |
| 30 | +myHashMap.put(2, 2); // The map is now [[1,1], [2,2]] |
| 31 | +myHashMap.get(1); // return 1, The map is now [[1,1], [2,2]] |
| 32 | +myHashMap.get(3); // return -1 (i.e., not found), The map is now [[1,1], [2,2]] |
| 33 | +myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value) |
| 34 | +myHashMap.get(2); // return 1, The map is now [[1,1], [2,1]] |
| 35 | +myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]] |
| 36 | +myHashMap.get(2); // return -1 (i.e., not found), The map is now [[1,1]] |
| 37 | + |
| 38 | +
|
| 39 | +Constraints: |
| 40 | +
|
| 41 | +0 <= key, value <= 106 |
| 42 | +At most 104 calls will be made to put, get, and remove. |
| 43 | +``` |
| 44 | + |
| 45 | +# Solution |
| 46 | + |
| 47 | +```python |
| 48 | + def __init__(self): |
| 49 | + """ |
| 50 | + Initialize your data structure here. |
| 51 | + """ |
| 52 | + self.cnt = 1000 |
| 53 | + self.bucket = [None] * self.cnt |
| 54 | + |
| 55 | + def put(self, key: int, value: int) -> None: |
| 56 | + """ |
| 57 | + value will always be non-negative. |
| 58 | + """ |
| 59 | + idx = key % self.cnt |
| 60 | + item = self.bucket[idx] |
| 61 | + if item is None: |
| 62 | + self.bucket[idx] = [[key, value]] |
| 63 | + else: |
| 64 | + for i in item: |
| 65 | + if key == i[0]: |
| 66 | + i[1] = value |
| 67 | + return |
| 68 | + item.append([key, value]) |
| 69 | + |
| 70 | + def get(self, key: int) -> int: |
| 71 | + """ |
| 72 | + Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key |
| 73 | + """ |
| 74 | + idx = key % self.cnt |
| 75 | + item = self.bucket[idx] |
| 76 | + if item: |
| 77 | + for k, v in item: |
| 78 | + if k == key: return v |
| 79 | + return -1 |
| 80 | + |
| 81 | + def remove(self, key: int) -> None: |
| 82 | + """ |
| 83 | + Removes the mapping of the specified value key if this map contains a mapping for the key |
| 84 | + """ |
| 85 | + idx = key % self.cnt |
| 86 | + item = self.bucket[idx] |
| 87 | + if item: |
| 88 | + for pos, i in enumerate(item): |
| 89 | + if i[0] == key: |
| 90 | + del item[pos] |
| 91 | + return |
| 92 | +``` |
0 commit comments