Leetcode # 706. Design HashMap
- 2023.07.26
- ★ Easy Hash Function Hash Table LeetCode
https://leetcode.com/problems/design-hashmap
Solution
以 modulo 作為 hash function
作為 modulo 的除數
我選擇了六位數中最小的質數 100003
同時這也會變成 hash table 的容量
Time Complexity: O()
Space Complexity: O()
(The input and output generally do not count towards the space complexity.)
class MyHashMap: def __init__(self): self.base = 100003 self.map = [-1] * self.base def put(self, key: int, value: int) -> None: self.map[key % self.base] = value def get(self, key: int) -> int: return self.map[key % self.base] def remove(self, key: int) -> None: self.map[key % self.base] = -1
Last Updated on 2023/08/16 by A1go