mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 03:30:30 +08:00
build
This commit is contained in:
@@ -1075,7 +1075,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
def array_traversal(nums: List[int]) -> int:
|
||||
def array_traversal(nums: list[int]) -> int:
|
||||
""" 线性阶(遍历数组)"""
|
||||
count: int = 0
|
||||
# 循环次数与数组长度成正比
|
||||
@@ -1391,7 +1391,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="time_complexity.py"
|
||||
def bubble_sort(nums: List[int]) -> int:
|
||||
def bubble_sort(nums: list[int]) -> int:
|
||||
""" 平方阶(冒泡排序)"""
|
||||
count: int = 0 # 计数器
|
||||
# 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
@@ -2481,15 +2481,15 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title="worst_best_time_complexity.py"
|
||||
def random_numbers(n: int) -> List[int]:
|
||||
def random_numbers(n: int) -> list[int]:
|
||||
""" 生成一个数组,元素为: 1, 2, ..., n ,顺序被打乱 """
|
||||
# 生成数组 nums =: 1, 2, 3, ..., n
|
||||
nums: List[int] = [i for i in range(1, n + 1)]
|
||||
nums: list[int] = [i for i in range(1, n + 1)]
|
||||
# 随机打乱数组元素
|
||||
random.shuffle(nums)
|
||||
return nums
|
||||
|
||||
def find_one(nums: List[int]) -> int:
|
||||
def find_one(nums: list[int]) -> int:
|
||||
""" 查找数组 nums 中数字 1 所在索引 """
|
||||
for i in range(len(nums)):
|
||||
# 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
|
||||
Reference in New Issue
Block a user