Polish the content

Polish the chapter preface, introduction and complexity anlysis
This commit is contained in:
krahets
2023-08-08 23:16:33 +08:00
parent 9ed16db68e
commit 932d14644d
26 changed files with 215 additions and 182 deletions

View File

@ -35,9 +35,9 @@ def linear(n: int):
# 长度为 n 的列表占用 O(n) 空间
nums = [0] * n
# 长度为 n 的哈希表占用 O(n) 空间
mapp = dict[int, str]()
hmap = dict[int, str]()
for i in range(n):
mapp[i] = str(i)
hmap[i] = str(i)
def linear_recur(n: int):

View File

@ -61,7 +61,7 @@ def exponential(n: int) -> int:
"""指数阶(循环实现)"""
count = 0
base = 1
# cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
# 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for _ in range(n):
for _ in range(base):
count += 1

View File

@ -80,38 +80,38 @@ class ArrayHashMap:
"""Driver Code"""
if __name__ == "__main__":
# 初始化哈希表
mapp = ArrayHashMap()
hmap = ArrayHashMap()
# 添加操作
# 在哈希表中添加键值对 (key, value)
mapp.put(12836, "小哈")
mapp.put(15937, "小啰")
mapp.put(16750, "小算")
mapp.put(13276, "小法")
mapp.put(10583, "小鸭")
hmap.put(12836, "小哈")
hmap.put(15937, "小啰")
hmap.put(16750, "小算")
hmap.put(13276, "小法")
hmap.put(10583, "小鸭")
print("\n添加完成后,哈希表为\nKey -> Value")
mapp.print()
hmap.print()
# 查询操作
# 向哈希表输入键 key ,得到值 value
name = mapp.get(15937)
name = hmap.get(15937)
print("\n输入学号 15937 ,查询到姓名 " + name)
# 删除操作
# 在哈希表中删除键值对 (key, value)
mapp.remove(10583)
hmap.remove(10583)
print("\n删除 10583 后,哈希表为\nKey -> Value")
mapp.print()
hmap.print()
# 遍历哈希表
print("\n遍历键值对 Key->Value")
for pair in mapp.entry_set():
for pair in hmap.entry_set():
print(pair.key, "->", pair.val)
print("\n单独遍历键 Key")
for key in mapp.key_set():
for key in hmap.key_set():
print(key)
print("\n单独遍历值 Value")
for val in mapp.value_set():
for val in hmap.value_set():
print(val)

View File

@ -12,38 +12,38 @@ from modules import *
"""Driver Code"""
if __name__ == "__main__":
# 初始化哈希表
mapp = dict[int, str]()
hmap = dict[int, str]()
# 添加操作
# 在哈希表中添加键值对 (key, value)
mapp[12836] = "小哈"
mapp[15937] = "小啰"
mapp[16750] = "小算"
mapp[13276] = "小法"
mapp[10583] = "小鸭"
hmap[12836] = "小哈"
hmap[15937] = "小啰"
hmap[16750] = "小算"
hmap[13276] = "小法"
hmap[10583] = "小鸭"
print("\n添加完成后,哈希表为\nKey -> Value")
print_dict(mapp)
print_dict(hmap)
# 查询操作
# 向哈希表输入键 key ,得到值 value
name: str = mapp[15937]
name: str = hmap[15937]
print("\n输入学号 15937 ,查询到姓名 " + name)
# 删除操作
# 在哈希表中删除键值对 (key, value)
mapp.pop(10583)
hmap.pop(10583)
print("\n删除 10583 后,哈希表为\nKey -> Value")
print_dict(mapp)
print_dict(hmap)
# 遍历哈希表
print("\n遍历键值对 Key->Value")
for key, value in mapp.items():
for key, value in hmap.items():
print(key, "->", value)
print("\n单独遍历键 Key")
for key in mapp.keys():
for key in hmap.keys():
print(key)
print("\n单独遍历值 Value")
for val in mapp.values():
for val in hmap.values():
print(val)

View File

@ -10,20 +10,20 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from modules import *
def hashing_search_array(mapp: dict[int, int], target: int) -> int:
def hashing_search_array(hmap: dict[int, int], target: int) -> int:
"""哈希查找(数组)"""
# 哈希表的 key: 目标元素value: 索引
# 若哈希表中无此 key ,返回 -1
return mapp.get(target, -1)
return hmap.get(target, -1)
def hashing_search_linkedlist(
mapp: dict[int, ListNode], target: int
hmap: dict[int, ListNode], target: int
) -> ListNode | None:
"""哈希查找(链表)"""
# 哈希表的 key: 目标元素value: 节点对象
# 若哈希表中无此 key ,返回 None
return mapp.get(target, None)
return hmap.get(target, None)
"""Driver Code"""

View File

@ -67,9 +67,9 @@ def print_tree(
print_tree(root.left, trunk, False)
def print_dict(mapp: dict):
def print_dict(hmap: dict):
"""Print a dict"""
for key, value in mapp.items():
for key, value in hmap.items():
print(key, "->", value)