class Node:
def __init__(self,key,value,next=None):
self.key = key
self.value = value
self.next = next
class MyHashMap:
def __init__(self):
self.array = [None]*(10**3)
def put(self, key: int, value: int) -> None:
index = key%(10**3)
if not self.array[index]:
self.array[index] = Node(key,value)
else:
cur = self.array[index]
while cur and cur.next:
if cur.key == key:
cur.value = value
return None
cur = cur.next
if cur.key == key:
cur.value = value
else:
cur.next = Node(key,value)
def get(self, key: int) -> int:
index = key%(10**3)
if not self.array[index]:
return -1
if self.array[index].key == key:
return self.array[index].value
else:
cur = self.array[index]
while cur and cur.key != key:
cur = cur.next
if not cur:
return -1
else:
return cur.value
def remove(self, key: int) -> None:
index = key%(10**3)
if not self.array[index]:
return None
if self.array[index].key == key:
self.array[index] = self.array[index].next
else:
cur = self.array[index]
while cur.next and cur.next.key != key:
cur = cur.next
if cur.next and cur.next.key == key:
cur.next = cur.next.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)
标签:index,哈希,映射,Python,self,next,key,array,cur
From: https://www.cnblogs.com/DCFV/p/18430910