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:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -9,20 +9,20 @@ package chapter_divide_and_conquer;
public class binary_search_recur {
/* Binary search: problem f(i, j) */
static int dfs(int[] nums, int target, int i, int 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
int 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);
} else if (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;
}
}
@@ -30,7 +30,7 @@ public class binary_search_recur {
/* Binary search */
static int binarySearch(int[] nums, int target) {
int n = nums.length;
// Solve the problem f(0, n-1)
// Solve problem f(0, n-1)
return dfs(nums, target, 0, n - 1);
}
@@ -38,7 +38,7 @@ public class binary_search_recur {
int target = 6;
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
// Binary search (closed interval on both sides)
// Binary search (closed interval)
int index = binarySearch(nums, target);
System.out.println("Index of target element 6 = " + index);
}

View File

@@ -10,14 +10,14 @@ import utils.*;
import java.util.*;
public class build_tree {
/* Build binary tree: divide and conquer */
/* Build a binary tree: divide and conquer */
static TreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {
// Terminate when the subtree interval is empty
// Stop when the subtree interval is empty
if (r - l < 0)
return null;
// Initialize the root node
TreeNode root = new TreeNode(preorder[i]);
// Query m to divide the left and right subtrees
// Look up m to separate the left and right subtrees
int m = inorderMap.get(preorder[i]);
// Subproblem: build the left subtree
root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
@@ -27,9 +27,9 @@ public class build_tree {
return root;
}
/* Build binary tree */
/* Build a binary tree */
static TreeNode buildTree(int[] preorder, int[] inorder) {
// Initialize hash map, storing the mapping from inorder elements to indices
// Initialize a hash table mapping each element in inorder to its index
Map<Integer, Integer> inorderMap = new HashMap<>();
for (int i = 0; i < inorder.length; i++) {
inorderMap.put(inorder[i], i);

View File

@@ -7,7 +7,7 @@
package chapter_divide_and_conquer;
public class fast_power {
/* Exponentiation by squaring */
/* Fast exponentiation */
static int fastPow(int x, int n) {
if (n == 0) {
return 1;

View File

@@ -9,9 +9,9 @@ package chapter_divide_and_conquer;
import java.util.*;
public class hanota {
/* Move a disk */
/* Move one disk */
static void move(List<Integer> src, List<Integer> tar) {
// Take out a disk from the top of src
// Take a disk from the top of src
Integer pan = src.remove(src.size() - 1);
// Place the disk on top of tar
tar.add(pan);
@@ -19,14 +19,14 @@ public class hanota {
/* Solve the Tower of Hanoi problem f(i) */
static void dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {
// If there is only one disk left in src, move it directly to tar
// If only one disk remains to be moved from src, move it directly 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);
@@ -40,18 +40,18 @@ public class hanota {
}
public static void main(String[] args) {
// The tail of the list is the top of the rod
// The end of each list represents the top of the peg
List<Integer> A = new ArrayList<>(Arrays.asList(5, 4, 3, 2, 1));
List<Integer> B = new ArrayList<>();
List<Integer> C = new ArrayList<>();
System.out.println("In initial state:");
System.out.println("Initial state:");
System.out.println("A = " + A);
System.out.println("B = " + B);
System.out.println("C = " + C);
solveHanota(A, B, C);
System.out.println("After disk movement is complete:");
System.out.println("After moving the disks:");
System.out.println("A = " + A);
System.out.println("B = " + B);
System.out.println("C = " + C);