mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-03 10:53:35 +08:00
Several bug fixes and improvements (#945)
* Update Dockerfile for code debugging. * Format Python code using Black. * Improve dark theme by defining html classes for the figures, animations and cover images. * Fix several glossary translation. * Update a code comment. * Fix climbing_stairs_backtrack: the pruning should not require the sorted choices list. * Update the code of array and list traversal. * Fix a rendering issue of README.md * Update code of list traversal. * Fix array_definition.png * Update README.md * Fix max_capacity_moving_short_board.png * Fix array.dart * Fix array.dart * Fix array.dart * Fix array.dart
This commit is contained in:
@@ -50,13 +50,14 @@ def traverse(nums: list[int]):
|
||||
count = 0
|
||||
# 通过索引遍历数组
|
||||
for i in range(len(nums)):
|
||||
count += 1
|
||||
# 直接遍历数组
|
||||
count += nums[i]
|
||||
# 直接遍历数组元素
|
||||
for num in nums:
|
||||
count += 1
|
||||
count += num
|
||||
# 同时遍历数据索引和元素
|
||||
for i, num in enumerate(nums):
|
||||
count += 1
|
||||
count += nums[i]
|
||||
count += num
|
||||
|
||||
|
||||
def find(nums: list[int], target: int) -> int:
|
||||
|
||||
@@ -38,16 +38,13 @@ if __name__ == "__main__":
|
||||
nums.pop(3)
|
||||
print("\n删除索引 3 处的元素,得到 nums =", nums)
|
||||
|
||||
# 遍历列表
|
||||
tmp = []
|
||||
# 通过索引遍历列表
|
||||
count = 0
|
||||
for i in range(len(nums)):
|
||||
tmp.append(nums[i])
|
||||
print(f"\n通过索引遍历列表得到 tmp = {tmp}")
|
||||
|
||||
tmp.clear()
|
||||
count += nums[i]
|
||||
# 直接遍历列表元素
|
||||
for num in nums:
|
||||
tmp.append(num)
|
||||
print(f"\n直接遍历列表元素得到 tmp = {tmp}")
|
||||
count += num
|
||||
|
||||
# 拼接两个列表
|
||||
nums1 = [6, 8, 7, 10, 9]
|
||||
|
||||
@@ -14,7 +14,7 @@ def backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int:
|
||||
for choice in choices:
|
||||
# 剪枝:不允许越过第 n 阶
|
||||
if state + choice > n:
|
||||
break
|
||||
continue
|
||||
# 尝试:做出选择,更新状态
|
||||
backtrack(choices, state + choice, n, res)
|
||||
# 回退
|
||||
|
||||
@@ -35,7 +35,9 @@ def show_trunks(p: Trunk | None):
|
||||
print(p.str, end="")
|
||||
|
||||
|
||||
def print_tree(root: TreeNode | None, prev: Trunk | None = None, is_right: bool = False):
|
||||
def print_tree(
|
||||
root: TreeNode | None, prev: Trunk | None = None, is_right: bool = False
|
||||
):
|
||||
"""
|
||||
Print a binary tree
|
||||
This tree printer is borrowed from TECHIE DELIGHT
|
||||
|
||||
Reference in New Issue
Block a user