This commit is contained in:
krahets
2023-03-03 02:46:12 +08:00
parent 122805bdc9
commit cf9d102ed5
24 changed files with 320 additions and 314 deletions
@@ -624,8 +624,8 @@ $$
=== "Python"
```python title="space_complexity.py"
""" 常数阶 """
def constant(n):
""" 常数阶 """
# 常量、变量、对象占用 O(1) 空间
a = 0
nums = [0] * 10000
@@ -829,8 +829,8 @@ $$
=== "Python"
```python title="space_complexity.py"
""" 线性阶 """
def linear(n):
""" 线性阶 """
# 长度为 n 的列表占用 O(n) 空间
nums = [0] * n
# 长度为 n 的哈希表占用 O(n) 空间
@@ -996,8 +996,8 @@ $$
=== "Python"
```python title="space_complexity.py"
""" 线性阶(递归实现) """
def linear_recur(n):
""" 线性阶(递归实现) """
print("递归 n =", n)
if n == 1: return
linear_recur(n - 1)
@@ -1127,8 +1127,8 @@ $$
=== "Python"
```python title="space_complexity.py"
""" 平方阶 """
def quadratic(n):
""" 平方阶 """
# 二维列表占用 O(n^2) 空间
num_matrix = [[0] * n for _ in range(n)]
```
@@ -1171,7 +1171,9 @@ $$
/* 平方阶 */
function quadratic(n: number): void {
// 矩阵占用 O(n^2) 空间
const numMatrix = Array(n).fill(null).map(() => Array(n).fill(null));
const numMatrix = Array(n)
.fill(null)
.map(() => Array(n).fill(null));
// 二维列表占用 O(n^2) 空间
const numList = [];
for (let i = 0; i < n; i++) {
@@ -1273,8 +1275,8 @@ $$
=== "Python"
```python title="space_complexity.py"
""" 平方阶(递归实现) """
def quadratic_recur(n):
""" 平方阶(递归实现) """
if n <= 0: return 0
# 数组 nums 长度为 n, n-1, ..., 2, 1
nums = [0] * n
@@ -1403,8 +1405,8 @@ $$
=== "Python"
```python title="space_complexity.py"
""" 指数阶(建立满二叉树) """
def build_tree(n):
""" 指数阶(建立满二叉树) """
if n == 0: return None
root = TreeNode(0)
root.left = build_tree(n - 1)