mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-12 07:26:07 +00:00
build
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -65,8 +65,8 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="leetcode_two_sum.py"
|
||||
""" 方法一:暴力枚举 """
|
||||
def two_sum_brute_force(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)):
|
||||
@@ -242,8 +242,8 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="leetcode_two_sum.py"
|
||||
""" 方法二:辅助哈希表 """
|
||||
def two_sum_hash_table(nums: List[int], target: int) -> List[int]:
|
||||
""" 方法二:辅助哈希表 """
|
||||
# 辅助哈希表,空间复杂度 O(n)
|
||||
dic = {}
|
||||
# 单层循环,时间复杂度 O(n)
|
||||
|
||||
@@ -821,8 +821,8 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 常数阶 """
|
||||
def constant(n):
|
||||
""" 常数阶 """
|
||||
count = 0
|
||||
size = 100000
|
||||
for _ in range(size):
|
||||
@@ -949,8 +949,8 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 线性阶 """
|
||||
def linear(n):
|
||||
""" 线性阶 """
|
||||
count = 0
|
||||
for _ in range(n):
|
||||
count += 1
|
||||
@@ -1075,8 +1075,8 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 线性阶(遍历数组)"""
|
||||
def array_traversal(nums):
|
||||
""" 线性阶(遍历数组)"""
|
||||
count = 0
|
||||
# 循环次数与数组长度成正比
|
||||
for num in nums:
|
||||
@@ -1215,8 +1215,8 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 平方阶 """
|
||||
def quadratic(n):
|
||||
""" 平方阶 """
|
||||
count = 0
|
||||
# 循环次数与数组长度成平方关系
|
||||
for i in range(n):
|
||||
@@ -1391,8 +1391,8 @@ $$
|
||||
=== "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):
|
||||
@@ -1604,8 +1604,8 @@ $$
|
||||
=== "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):
|
||||
@@ -1768,8 +1768,8 @@ $$
|
||||
=== "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
|
||||
```
|
||||
@@ -1884,8 +1884,8 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 对数阶(循环实现)"""
|
||||
def logarithmic(n):
|
||||
""" 对数阶(循环实现)"""
|
||||
count = 0
|
||||
while n > 1:
|
||||
n = n / 2
|
||||
@@ -2017,8 +2017,8 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 对数阶(递归实现)"""
|
||||
def log_recur(n):
|
||||
""" 对数阶(递归实现)"""
|
||||
if n <= 1: return 0
|
||||
return log_recur(n / 2) + 1
|
||||
```
|
||||
@@ -2133,8 +2133,8 @@ $$
|
||||
=== "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)
|
||||
@@ -2290,8 +2290,8 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
""" 阶乘阶(递归实现)"""
|
||||
def factorial_recur(n):
|
||||
""" 阶乘阶(递归实现)"""
|
||||
if n == 0: return 1
|
||||
count = 0
|
||||
# 从 1 个分裂出 n 个
|
||||
@@ -2480,16 +2480,16 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="worst_best_time_complexity.py"
|
||||
""" 生成一个数组,元素为: 1, 2, ..., n ,顺序被打乱 """
|
||||
def random_numbers(n):
|
||||
""" 生成一个数组,元素为: 1, 2, ..., n ,顺序被打乱 """
|
||||
# 生成数组 nums =: 1, 2, 3, ..., n
|
||||
nums = [i for i in range(1, n + 1)]
|
||||
# 随机打乱数组元素
|
||||
random.shuffle(nums)
|
||||
return nums
|
||||
|
||||
""" 查找数组 nums 中数字 1 所在索引 """
|
||||
def find_one(nums):
|
||||
""" 查找数组 nums 中数字 1 所在索引 """
|
||||
for i in range(len(nums)):
|
||||
# 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
# 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
|
||||
Reference in New Issue
Block a user