mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-07 15:01:58 +08:00
refactor: Follow the PEP 585 Typing standard (#439)
* Follow the PEP 585 Typing standard * Update list.py
This commit is contained in:
@ -17,7 +17,7 @@ def constant(n: int) -> None:
|
||||
""" 常数阶 """
|
||||
# 常量、变量、对象占用 O(1) 空间
|
||||
a: int = 0
|
||||
nums: List[int] = [0] * 10000
|
||||
nums: list[int] = [0] * 10000
|
||||
node = ListNode(0)
|
||||
# 循环中的变量占用 O(1) 空间
|
||||
for _ in range(n):
|
||||
@ -29,9 +29,9 @@ def constant(n: int) -> None:
|
||||
def linear(n: int) -> None:
|
||||
""" 线性阶 """
|
||||
# 长度为 n 的列表占用 O(n) 空间
|
||||
nums: List[int] = [0] * n
|
||||
nums: list[int] = [0] * n
|
||||
# 长度为 n 的哈希表占用 O(n) 空间
|
||||
mapp: Dict = {}
|
||||
mapp = dict[int, str]()
|
||||
for i in range(n):
|
||||
mapp[i] = str(i)
|
||||
|
||||
@ -44,16 +44,16 @@ def linear_recur(n: int) -> None:
|
||||
def quadratic(n: int) -> None:
|
||||
""" 平方阶 """
|
||||
# 二维列表占用 O(n^2) 空间
|
||||
num_matrix: List[List[int]] = [[0] * n for _ in range(n)]
|
||||
num_matrix: list[list[int]] = [[0] * n for _ in range(n)]
|
||||
|
||||
def quadratic_recur(n: int) -> int:
|
||||
""" 平方阶(递归实现) """
|
||||
if n <= 0: return 0
|
||||
# 数组 nums 长度为 n, n-1, ..., 2, 1
|
||||
nums: List[int] = [0] * n
|
||||
nums: list[int] = [0] * n
|
||||
return quadratic_recur(n - 1)
|
||||
|
||||
def build_tree(n: int) -> Optional[TreeNode]:
|
||||
def build_tree(n: int) -> TreeNode | None:
|
||||
""" 指数阶(建立满二叉树) """
|
||||
if n == 0: return None
|
||||
root = TreeNode(0)
|
||||
@ -74,5 +74,5 @@ if __name__ == "__main__":
|
||||
quadratic(n)
|
||||
quadratic_recur(n)
|
||||
# 指数阶
|
||||
root: Optional[TreeNode] = build_tree(n)
|
||||
root = build_tree(n)
|
||||
print_tree(root)
|
||||
|
Reference in New Issue
Block a user