This commit is contained in:
krahets
2023-04-09 05:12:22 +08:00
parent 01d05cc1f0
commit 37f11aff68
27 changed files with 265 additions and 248 deletions
@@ -626,7 +626,7 @@ $$
```python title="space_complexity.py"
def constant(n: int) -> None:
""" 常数阶 """
"""常数阶"""
# 常量、变量、对象占用 O(1) 空间
a: int = 0
nums: list[int] = [0] * 10000
@@ -831,7 +831,7 @@ $$
```python title="space_complexity.py"
def linear(n: int) -> None:
""" 线性阶 """
"""线性阶"""
# 长度为 n 的列表占用 O(n) 空间
nums: list[int] = [0] * n
# 长度为 n 的哈希表占用 O(n) 空间
@@ -998,9 +998,10 @@ $$
```python title="space_complexity.py"
def linear_recur(n: int) -> None:
""" 线性阶(递归实现) """
"""线性阶(递归实现)"""
print("递归 n =", n)
if n == 1: return
if n == 1:
return
linear_recur(n - 1)
```
@@ -1129,7 +1130,7 @@ $$
```python title="space_complexity.py"
def quadratic(n: int) -> None:
""" 平方阶 """
"""平方阶"""
# 二维列表占用 O(n^2) 空间
num_matrix: list[list[int]] = [[0] * n for _ in range(n)]
```
@@ -1277,8 +1278,9 @@ $$
```python title="space_complexity.py"
def quadratic_recur(n: int) -> int:
""" 平方阶(递归实现) """
if n <= 0: return 0
"""平方阶(递归实现)"""
if n <= 0:
return 0
# 数组 nums 长度为 n, n-1, ..., 2, 1
nums: list[int] = [0] * n
return quadratic_recur(n - 1)
@@ -1407,8 +1409,9 @@ $$
```python title="space_complexity.py"
def build_tree(n: int) -> TreeNode | None:
""" 指数阶(建立满二叉树) """
if n == 0: return None
"""指数阶(建立满二叉树)"""
if n == 0:
return None
root = TreeNode(0)
root.left = build_tree(n - 1)
root.right = build_tree(n - 1)