mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-14 09:48:31 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -7,34 +7,34 @@ Author: krahets (krahets@163.com)
|
||||
|
||||
def dfs(nums: list[int], target: int, i: int, j: int) -> int:
|
||||
"""Binary search: problem f(i, j)"""
|
||||
# If the interval is empty, it means there is no target element, return -1
|
||||
# If the interval is empty, the target is absent; return -1
|
||||
if i > j:
|
||||
return -1
|
||||
# Calculate the midpoint index m
|
||||
m = (i + j) // 2
|
||||
if nums[m] < target:
|
||||
# Recursion subproblem f(m+1, j)
|
||||
# Recurse on subproblem f(m+1, j)
|
||||
return dfs(nums, target, m + 1, j)
|
||||
elif nums[m] > target:
|
||||
# Recursion subproblem f(i, m-1)
|
||||
# Recurse on subproblem f(i, m-1)
|
||||
return dfs(nums, target, i, m - 1)
|
||||
else:
|
||||
# Found the target element, return its index
|
||||
# The target element was found; return its index
|
||||
return m
|
||||
|
||||
|
||||
def binary_search(nums: list[int], target: int) -> int:
|
||||
"""Binary search"""
|
||||
n = len(nums)
|
||||
# Solve the problem f(0, n-1)
|
||||
# Solve problem f(0, n-1)
|
||||
return dfs(nums, target, 0, n - 1)
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
"""Driver code"""
|
||||
if __name__ == "__main__":
|
||||
target = 6
|
||||
nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]
|
||||
|
||||
# Binary search (closed interval on both sides)
|
||||
# Binary search (closed interval)
|
||||
index = binary_search(nums, target)
|
||||
print("Index of target element 6 = ", index)
|
||||
|
||||
@@ -18,13 +18,13 @@ def dfs(
|
||||
l: int,
|
||||
r: int,
|
||||
) -> TreeNode | None:
|
||||
"""Build binary tree: divide and conquer"""
|
||||
# Terminate when the subtree interval is empty
|
||||
"""Build a binary tree: divide and conquer"""
|
||||
# Stop when the subtree interval is empty
|
||||
if r - l < 0:
|
||||
return None
|
||||
# Initialize the root node
|
||||
root = TreeNode(preorder[i])
|
||||
# Query m to divide the left and right subtrees
|
||||
# Look up m to separate the left and right subtrees
|
||||
m = inorder_map[preorder[i]]
|
||||
# Subproblem: build the left subtree
|
||||
root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)
|
||||
@@ -35,14 +35,14 @@ def dfs(
|
||||
|
||||
|
||||
def build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None:
|
||||
"""Build binary tree"""
|
||||
# Initialize hash map, storing the mapping from inorder elements to indices
|
||||
"""Build a binary tree"""
|
||||
# Initialize a hash table mapping each element in inorder to its index
|
||||
inorder_map = {val: i for i, val in enumerate(inorder)}
|
||||
root = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1)
|
||||
return root
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
"""Driver code"""
|
||||
if __name__ == "__main__":
|
||||
preorder = [3, 9, 2, 1, 7]
|
||||
inorder = [9, 3, 1, 2, 7]
|
||||
@@ -50,5 +50,5 @@ if __name__ == "__main__":
|
||||
print(f"Inorder traversal = {inorder}")
|
||||
|
||||
root = build_tree(preorder, inorder)
|
||||
print("The built binary tree is:")
|
||||
print("The constructed binary tree is:")
|
||||
print_tree(root)
|
||||
|
||||
@@ -6,7 +6,7 @@ Author: Hello Algo Team
|
||||
|
||||
|
||||
def fast_pow(x: int, n: int) -> int:
|
||||
"""Exponentiation by squaring"""
|
||||
"""Fast exponentiation"""
|
||||
if n == 0:
|
||||
return 1
|
||||
half = fast_pow(x, n // 2)
|
||||
|
||||
@@ -6,8 +6,8 @@ Author: krahets (krahets@163.com)
|
||||
|
||||
|
||||
def move(src: list[int], tar: list[int]):
|
||||
"""Move a disk"""
|
||||
# Take out a disk from the top of src
|
||||
"""Move one disk"""
|
||||
# Take a disk from the top of src
|
||||
pan = src.pop()
|
||||
# Place the disk on top of tar
|
||||
tar.append(pan)
|
||||
@@ -15,13 +15,13 @@ def move(src: list[int], tar: list[int]):
|
||||
|
||||
def dfs(i: int, src: list[int], buf: list[int], tar: list[int]):
|
||||
"""Solve the Tower of Hanoi problem f(i)"""
|
||||
# If there is only one disk left in src, move it directly to tar
|
||||
# If this subproblem has only one disk to move, move it directly from src to tar
|
||||
if i == 1:
|
||||
move(src, tar)
|
||||
return
|
||||
# Subproblem f(i-1): move the top i-1 disks from src to buf using tar
|
||||
dfs(i - 1, src, tar, buf)
|
||||
# Subproblem f(1): move the remaining disk from src to tar
|
||||
# Subproblem f(1): move the remaining disk of this subproblem from src to tar
|
||||
move(src, tar)
|
||||
# Subproblem f(i-1): move the top i-1 disks from buf to tar using src
|
||||
dfs(i - 1, buf, src, tar)
|
||||
@@ -34,9 +34,9 @@ def solve_hanota(A: list[int], B: list[int], C: list[int]):
|
||||
dfs(n, A, B, C)
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
"""Driver code"""
|
||||
if __name__ == "__main__":
|
||||
# The tail of the list is the top of the rod
|
||||
# The end of each list represents the top of the peg
|
||||
A = [5, 4, 3, 2, 1]
|
||||
B = []
|
||||
C = []
|
||||
|
||||
Reference in New Issue
Block a user