mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
Format C, C++, C#, Go, Java, Python, Rust code.
This commit is contained in:
@ -46,7 +46,7 @@ def bubble_sort(nums: list[int]) -> int:
|
||||
count = 0 # 计数器
|
||||
# 外循环:未排序区间为 [0, i]
|
||||
for i in range(len(nums) - 1, 0, -1):
|
||||
# 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
||||
# 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
||||
for j in range(i):
|
||||
if nums[j] > nums[j + 1]:
|
||||
# 交换 nums[j] 与 nums[j + 1]
|
||||
|
||||
@ -27,7 +27,7 @@ def dfs(i: int, src: list[int], buf: list[int], tar: list[int]):
|
||||
dfs(i - 1, buf, src, tar)
|
||||
|
||||
|
||||
def hanota(A: list[int], B: list[int], C: list[int]):
|
||||
def solve_hanota(A: list[int], B: list[int], C: list[int]):
|
||||
"""求解汉诺塔"""
|
||||
n = len(A)
|
||||
# 将 A 顶部 n 个圆盘借助 B 移到 C
|
||||
@ -45,7 +45,7 @@ if __name__ == "__main__":
|
||||
print(f"B = {B}")
|
||||
print(f"C = {C}")
|
||||
|
||||
hanota(A, B, C)
|
||||
solve_hanota(A, B, C)
|
||||
|
||||
print("圆盘移动完成后:")
|
||||
print(f"A = {A}")
|
||||
|
||||
@ -20,7 +20,7 @@ if __name__ == "__main__":
|
||||
print(f"布尔量 {bol} 的哈希值为 {hash_bol}")
|
||||
|
||||
dec = 3.14159
|
||||
hash_dec = hash(dec)
|
||||
hash_dec = hash(dec)
|
||||
print(f"小数 {dec} 的哈希值为 {hash_dec}")
|
||||
|
||||
str = "Hello 算法"
|
||||
|
||||
@ -33,5 +33,5 @@ if __name__ == "__main__":
|
||||
k = 3
|
||||
|
||||
res = top_k_heap(nums, k)
|
||||
print(f"最大的 {k} 个元素为")
|
||||
print(f"最大的 {k} 个元素为")
|
||||
print_heap(res)
|
||||
|
||||
@ -10,7 +10,7 @@ def bubble_sort(nums: list[int]):
|
||||
n = len(nums)
|
||||
# 外循环:未排序区间为 [0, i]
|
||||
for i in range(n - 1, 0, -1):
|
||||
# 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
||||
# 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
||||
for j in range(i):
|
||||
if nums[j] > nums[j + 1]:
|
||||
# 交换 nums[j] 与 nums[j + 1]
|
||||
@ -23,7 +23,7 @@ def bubble_sort_with_flag(nums: list[int]):
|
||||
# 外循环:未排序区间为 [0, i]
|
||||
for i in range(n - 1, 0, -1):
|
||||
flag = False # 初始化标志位
|
||||
# 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
||||
# 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
||||
for j in range(i):
|
||||
if nums[j] > nums[j + 1]:
|
||||
# 交换 nums[j] 与 nums[j + 1]
|
||||
|
||||
@ -36,7 +36,7 @@ def heap_sort(nums: list[int]):
|
||||
nums[0], nums[i] = nums[i], nums[0]
|
||||
# 以根节点为起点,从顶至底进行堆化
|
||||
sift_down(nums, i, 0)
|
||||
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
|
||||
@ -35,9 +35,7 @@ def show_trunks(p: Trunk | None):
|
||||
print(p.str, end="")
|
||||
|
||||
|
||||
def print_tree(
|
||||
root: TreeNode | None, prev: Trunk | None = None, is_left: bool = False
|
||||
):
|
||||
def print_tree(root: TreeNode | None, prev: Trunk | None = None, is_left: bool = False):
|
||||
"""
|
||||
Print a binary tree
|
||||
This tree printer is borrowed from TECHIE DELIGHT
|
||||
|
||||
Reference in New Issue
Block a user