1. Add the building util of Python

for the markdown docs.
2. Update the deploy.sh
This commit is contained in:
krahets
2023-02-06 23:23:21 +08:00
parent 64f251f933
commit ea901af217
28 changed files with 292 additions and 933 deletions

View File

@@ -624,18 +624,7 @@ $$
=== "Python"
```python title="space_complexity.py"
""" 常数阶 """
def constant(n):
# 常量、变量、对象占用 O(1) 空间
a = 0
nums = [0] * 10000
node = ListNode(0)
# 循环中的变量占用 O(1) 空间
for _ in range(n):
c = 0
# 循环中的函数占用 O(1) 空间
for _ in range(n):
function()
[class]{}-[func]{constant}
```
=== "Go"
@@ -829,14 +818,7 @@ $$
=== "Python"
```python title="space_complexity.py"
""" 线性阶 """
def linear(n):
# 长度为 n 的列表占用 O(n) 空间
nums = [0] * n
# 长度为 n 的哈希表占用 O(n) 空间
mapp = {}
for i in range(n):
mapp[i] = str(i)
[class]{}-[func]{linear}
```
=== "Go"
@@ -996,11 +978,7 @@ $$
=== "Python"
```python title="space_complexity.py"
""" 线性阶(递归实现) """
def linear_recur(n):
print("递归 n =", n)
if n == 1: return
linear_recur(n - 1)
[class]{}-[func]{linear_recur}
```
=== "Go"
@@ -1127,10 +1105,7 @@ $$
=== "Python"
```python title="space_complexity.py"
""" 平方阶 """
def quadratic(n):
# 二维列表占用 O(n^2) 空间
num_matrix = [[0] * n for _ in range(n)]
[class]{}-[func]{quadratic}
```
=== "Go"
@@ -1272,12 +1247,7 @@ $$
=== "Python"
```python title="space_complexity.py"
""" 平方阶(递归实现) """
def quadratic_recur(n):
if n <= 0: return 0
# 数组 nums 长度为 n, n-1, ..., 2, 1
nums = [0] * n
return quadratic_recur(n - 1)
[class]{}-[func]{quadratic_recur}
```
=== "Go"
@@ -1400,13 +1370,7 @@ $$
=== "Python"
```python title="space_complexity.py"
""" 指数阶(建立满二叉树) """
def build_tree(n):
if n == 0: return None
root = TreeNode(0)
root.left = build_tree(n - 1)
root.right = build_tree(n - 1)
return root
[class]{}-[func]{build_tree}
```
=== "Go"

View File

@@ -70,14 +70,7 @@ comments: true
=== "Python"
```python title="leetcode_two_sum.py"
class SolutionBruteForce:
def twoSum(self, nums: List[int], target: int) -> List[int]:
# 两层循环,时间复杂度 O(n^2)
for i in range(len(nums) - 1):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return i, j
return []
[class]{SolutionBruteForce}-[func]{}
```
=== "Go"
@@ -247,16 +240,7 @@ comments: true
=== "Python"
```python title="leetcode_two_sum.py"
class SolutionHashMap:
def twoSum(self, nums: List[int], target: int) -> List[int]:
# 辅助哈希表,空间复杂度 O(n)
dic = {}
# 单层循环,时间复杂度 O(n)
for i in range(len(nums)):
if target - nums[i] in dic:
return dic[target - nums[i]], i
dic[nums[i]] = i
return []
[class]{SolutionHashMap}-[func]{}
```
=== "Go"

111
docs/chapter_computational_complexity/time_complexity.md Normal file → Executable file
View File

@@ -821,13 +821,7 @@ $$
=== "Python"
```python title="time_complexity.py"
""" 常数阶 """
def constant(n):
count = 0
size = 100000
for _ in range(size):
count += 1
return count
[class]{}-[func]{constant}
```
=== "Go"
@@ -958,12 +952,7 @@ $$
=== "Python"
```python title="time_complexity.py"
""" 线性阶 """
def linear(n):
count = 0
for _ in range(n):
count += 1
return count
[class]{}-[func]{linear}
```
=== "Go"
@@ -1091,13 +1080,7 @@ $$
=== "Python"
```python title="time_complexity.py"
""" 线性阶(遍历数组)"""
def array_traversal(nums):
count = 0
# 循环次数与数组长度成正比
for num in nums:
count += 1
return count
[class]{}-[func]{array_traversal}
```
=== "Go"
@@ -1239,14 +1222,7 @@ $$
=== "Python"
```python title="time_complexity.py"
""" 平方阶 """
def quadratic(n):
count = 0
# 循环次数与数组长度成平方关系
for i in range(n):
for j in range(n):
count += 1
return count
[class]{}-[func]{quadratic}
```
=== "Go"
@@ -1425,20 +1401,7 @@ $$
=== "Python"
```python title="time_complexity.py"
""" 平方阶(冒泡排序)"""
def bubble_sort(nums):
count = 0 # 计数器
# 外循环:待排序元素数量为 n-1, n-2, ..., 1
for i in range(len(nums) - 1, 0, -1):
# 内循环:冒泡操作
for j in range(i):
if nums[j] > nums[j + 1]:
# 交换 nums[j] 与 nums[j + 1]
tmp = nums[j]
nums[j] = nums[j + 1]
nums[j + 1] = tmp
count += 3 # 元素交换包含 3 个单元操作
return count
[class]{}-[func]{bubble_sort}
```
=== "Go"
@@ -1658,16 +1621,7 @@ $$
=== "Python"
```python title="time_complexity.py"
""" 指数阶(循环实现)"""
def exponential(n):
count, base = 0, 1
# cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for _ in range(n):
for _ in range(base):
count += 1
base *= 2
# count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count
[class]{}-[func]{exponential}
```
=== "Go"
@@ -1836,10 +1790,7 @@ $$
=== "Python"
```python title="time_complexity.py"
""" 指数阶(递归实现)"""
def exp_recur(n):
if n == 1: return 1
return exp_recur(n - 1) + exp_recur(n - 1) + 1
[class]{}-[func]{exp_recur}
```
=== "Go"
@@ -1957,13 +1908,7 @@ $$
=== "Python"
```python title="time_complexity.py"
""" 对数阶(循环实现)"""
def logarithmic(n):
count = 0
while n > 1:
n = n / 2
count += 1
return count
[class]{}-[func]{logarithmic}
```
=== "Go"
@@ -2099,10 +2044,7 @@ $$
=== "Python"
```python title="time_complexity.py"
""" 对数阶(递归实现)"""
def log_recur(n):
if n <= 1: return 0
return log_recur(n / 2) + 1
[class]{}-[func]{log_recur}
```
=== "Go"
@@ -2220,14 +2162,7 @@ $$
=== "Python"
```python title="time_complexity.py"
""" 线性对数阶 """
def linear_log_recur(n):
if n <= 1: return 1
count = linear_log_recur(n // 2) + \
linear_log_recur(n // 2)
for _ in range(n):
count += 1
return count
[class]{}-[func]{linear_log_recur}
```
=== "Go"
@@ -2387,14 +2322,7 @@ $$
=== "Python"
```python title="time_complexity.py"
""" 阶乘阶(递归实现)"""
def factorial_recur(n):
if n == 0: return 1
count = 0
# 从 1 个分裂出 n 个
for _ in range(n):
count += factorial_recur(n - 1)
return count
[class]{}-[func]{factorial_recur}
```
=== "Go"
@@ -2587,22 +2515,9 @@ $$
=== "Python"
```python title="worst_best_time_complexity.py"
""" 生成一个数组,元素为: 1, 2, ..., n ,顺序被打乱 """
def random_numbers(n):
# 生成数组 nums =: 1, 2, 3, ..., n
nums = [i for i in range(1, n + 1)]
# 随机打乱数组元素
random.shuffle(nums)
return nums
[class]{}-[func]{random_numbers}
""" 查找数组 nums 中数字 1 所在索引 """
def find_one(nums):
for i in range(len(nums)):
# 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
# 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
if nums[i] == 1:
return i
return -1
[class]{}-[func]{find_one}
```
=== "Go"