mirror of
https://github.com/krahets/hello-algo.git
synced 2025-10-28 12:36:43 +08:00
Update array_hash_map.py (#1803)
基于数组实现的哈希表中,`get()` 函数的返回值还可能会 None;且 `put()` 函数不仅可添加键值对、还可更新表中已有键值对的值;
This commit is contained in:
@ -26,7 +26,7 @@ class ArrayHashMap:
|
|||||||
index = key % 100
|
index = key % 100
|
||||||
return index
|
return index
|
||||||
|
|
||||||
def get(self, key: int) -> str:
|
def get(self, key: int) -> str | None:
|
||||||
"""查询操作"""
|
"""查询操作"""
|
||||||
index: int = self.hash_func(key)
|
index: int = self.hash_func(key)
|
||||||
pair: Pair = self.buckets[index]
|
pair: Pair = self.buckets[index]
|
||||||
@ -35,7 +35,7 @@ class ArrayHashMap:
|
|||||||
return pair.val
|
return pair.val
|
||||||
|
|
||||||
def put(self, key: int, val: str):
|
def put(self, key: int, val: str):
|
||||||
"""添加操作"""
|
"""添加和更新操作"""
|
||||||
pair = Pair(key, val)
|
pair = Pair(key, val)
|
||||||
index: int = self.hash_func(key)
|
index: int = self.hash_func(key)
|
||||||
self.buckets[index] = pair
|
self.buckets[index] = pair
|
||||||
|
|||||||
Reference in New Issue
Block a user