defput(self, key: int, value: int) -> None: index = key % self.size if self.hash[index] == None: # not in map yet self.hash[index] = ListNode(key, value) else: cur = self.hash[index] whileTrue: if cur.pair[0] == key: cur.pair = (key, value) return if cur.next == None: break cur = cur.next cur.next = ListNode(key, value)
defget(self, key: int) -> int: index = key % self.size if self.hash[index] == None: return-1 else: cur = self.hash[index] whileTrue: if cur.pair[0] == key: return cur.pair[1] if cur.next == None: return-1 cur = cur.next
defremove(self, key: int) -> None: index = key % self.size cur = pre = self.hash[index] ifnot cur: return if cur.pair[0] == key: self.hash[index] = self.hash[index].next else: cur = cur.next while cur: if cur.pair[0] == key: pre.next = cur.next break else: cur, pre = cur.next, pre.next # Your MyHashMap object will be instantiated and called as such: # obj = MyHashMap() # obj.put(key,value) # param_2 = obj.get(key) # obj.remove(key)
time complexity: $O()$ space complexity: $O()$ reference: related problem: