[
design
hash table
]
BinarySearch 0740 Set
Problem statement
https://binarysearch.com/problems/Set/
Solution
Equal to Leetcode 0705. Design HashSet, just change constant a bit to get AC.
Complexity
See complexities of leetcode problem.
Code
class CustomSet:
def eval_hash(self, key):
return ((key*1031237) & (1<<15) - 1)>>5
def __init__(self):
self.arr = [[] for _ in range(1<<10)]
def add(self, key: int) -> None:
t = self.eval_hash(key)
if key not in self.arr[t]:
self.arr[t].append(key)
def remove(self, key: int) -> None:
t = self.eval_hash(key)
if key in self.arr[t]:
self.arr[t].remove(key)
def exists(self, key: int) -> bool:
t = self.eval_hash(key)
return key in self.arr[t]