[
design
hash table
]
BinarySearch 0739 Hash Table
Problem statement
https://binarysearch.com/problems/Hash-Table/
Solution
Equal to Leetcode 0706. Design HashMap, just change constant a bit to get AC.
Complexity
See complexities of leetcode problem 0705/0706.
Code
class HashTable:
def eval_hash(self, key):
return ((key*1031237) & (1<<15) - 1)>>5
def __init__(self):
self.arr = [[] for _ in range(1<<10)]
def put(self, key, value):
t = self.eval_hash(key)
for i,(k,v) in enumerate(self.arr[t]):
if k == key:
self.arr[t][i] = (k, value)
return
self.arr[t].append((key, value))
def get(self, key):
t = self.eval_hash(key)
for i,(k,v) in enumerate(self.arr[t]):
if k == key: return v
return -1
def remove(self, key: int):
t = self.eval_hash(key)
for i,(k,v) in enumerate(self.arr[t]):
if k == key:
self.arr[t].remove((k,v))