Design HashMap — LeetCode 706 Python Solution
EasyDesignArrayHash TableLinked ListHash Function
- Problem
- #706
- Pattern
- Linked List
- Reading time
- 4 min
- Source
- leetcode.com
The problem
Design a HashMap without using any built-in hash table libraries. Implement the MyHashMap class: MyHashMap() initializes the object with an empty map.
Example
- Input
- ["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
- Output
- [null, null, null, 1, -1, null, 1, null, -1]
- Explanation
- MyHashMap myHashMap = new MyHashMap();
Python solution
Python
class MyHashMap:
def __init__(self):
self.data = [-1] * 1000001
def put(self, key: int, value: int) -> None:
self.data[key] = value
def get(self, key: int) -> int:
return self.data[key]
def remove(self, key: int) -> None:
self.data[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)Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(n) auxiliary |
Pattern: Linked List
Rewire pointers in place, with a dummy head and a saved next to keep it safe. LeetCode 706. Design HashMap is filed here because LeetCode tags it Linked List, which is the vocabulary this hub collects.
The linked list guide has the Python template for the pattern and the 75 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 706. Design HashMap?
- LeetCode 706. Design HashMap is rated Easy on LeetCode.
- What is the time complexity of LeetCode 706. Design HashMap?
- The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 706. Design HashMap?
- The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 706. Design HashMap cover?
- LeetCode 706. Design HashMap is tagged Design, Array, Hash Table, Linked List and Hash Function on LeetCode.