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,10 +9,10 @@ package chapter_backtracking;
import java.util.*;
public class n_queens {
/* Backtracking algorithm: N queens */
/* Backtracking: n-queens */
public static void backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,
boolean[] cols, boolean[] diags1, boolean[] diags2) {
// When all rows are placed, record the solution
// Record a solution when a queen has been placed in every row
if (row == n) {
List<List<String>> copyState = new ArrayList<>();
for (List<String> sRow : state) {
@@ -21,28 +21,28 @@ public class n_queens {
res.add(copyState);
return;
}
// Traverse all columns
// Iterate over all columns
for (int col = 0; col < n; col++) {
// Calculate the main diagonal and anti-diagonal corresponding to this cell
// Calculate the main-diagonal and anti-diagonal indices for this square
int diag1 = row - col + n - 1;
int diag2 = row + col;
// Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell
// Prune: the square must have no queen in its column, main diagonal, or anti-diagonal
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// Attempt: place the queen in this cell
// Try: place a queen on this square
state.get(row).set(col, "Q");
cols[col] = diags1[diag1] = diags2[diag2] = true;
// Place the next row
// Place a queen in the next row
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// Backtrack: restore this cell to an empty cell
// Backtrack: restore this square to empty
state.get(row).set(col, "#");
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
/* Solve N queens */
/* Solve the n-queens problem */
public static List<List<List<String>>> nQueens(int n) {
// Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell
// Initialize an n*n board, where 'Q' represents a queen and '#' represents an empty square
List<List<String>> state = new ArrayList<>();
for (int i = 0; i < n; i++) {
List<String> row = new ArrayList<>();
@@ -51,9 +51,9 @@ public class n_queens {
}
state.add(row);
}
boolean[] cols = new boolean[n]; // Record whether there is a queen in the column
boolean[] diags1 = new boolean[2 * n - 1]; // Record whether there is a queen on the main diagonal
boolean[] diags2 = new boolean[2 * n - 1]; // Record whether there is a queen on the anti-diagonal
boolean[] cols = new boolean[n]; // Track whether a column contains a queen
boolean[] diags1 = new boolean[2 * n - 1]; // Track whether a main diagonal contains a queen
boolean[] diags2 = new boolean[2 * n - 1]; // Track whether an anti-diagonal contains a queen
List<List<List<String>>> res = new ArrayList<>();
backtrack(0, n, state, res, cols, diags1, diags2);
@@ -65,8 +65,8 @@ public class n_queens {
int n = 4;
List<List<List<String>>> res = nQueens(n);
System.out.println("Input board size is " + n);
System.out.println("Total queen placement solutions: " + res.size() + "");
System.out.println("Input board side length: " + n);
System.out.println("There are " + res.size() + " queen placements");
for (List<List<String>> state : res) {
System.out.println("--------------------");
for (List<String> row : state) {

View File

@@ -9,24 +9,24 @@ package chapter_backtracking;
import java.util.*;
public class permutations_i {
/* Backtracking algorithm: Permutations I */
/* Backtracking: permutations I */
public static void backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {
// When the state length equals the number of elements, record the solution
// Record a solution when the state length equals the number of elements
if (state.size() == choices.length) {
res.add(new ArrayList<Integer>(state));
return;
}
// Traverse all choices
// Iterate over all choices
for (int i = 0; i < choices.length; i++) {
int choice = choices[i];
// Pruning: do not allow repeated selection of elements
// Prune: do not choose an element more than once
if (!selected[i]) {
// Attempt: make choice, update state
// Try: make a choice and update the state
selected[i] = true;
state.add(choice);
// Proceed to the next round of selection
// Proceed to the next choice
backtrack(state, choices, selected, res);
// Backtrack: undo choice, restore to previous state
// Backtrack: undo the choice and restore the previous state
selected[i] = false;
state.remove(state.size() - 1);
}
@@ -46,6 +46,6 @@ public class permutations_i {
List<List<Integer>> res = permutationsI(nums);
System.out.println("Input array nums = " + Arrays.toString(nums));
System.out.println("All permutations res = " + res);
System.out.println("All permutations: res = " + res);
}
}

View File

@@ -9,26 +9,26 @@ package chapter_backtracking;
import java.util.*;
public class permutations_ii {
/* Backtracking algorithm: Permutations II */
/* Backtracking: permutations II */
static void backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {
// When the state length equals the number of elements, record the solution
// Record a solution when the state length equals the number of elements
if (state.size() == choices.length) {
res.add(new ArrayList<Integer>(state));
return;
}
// Traverse all choices
// Iterate over all choices
Set<Integer> duplicated = new HashSet<Integer>();
for (int i = 0; i < choices.length; i++) {
int choice = choices[i];
// Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements
// Prune: do not reuse an element or choose the same value again at this depth
if (!selected[i] && !duplicated.contains(choice)) {
// Attempt: make choice, update state
duplicated.add(choice); // Record the selected element value
// Try: make a choice and update the state
duplicated.add(choice); // Record the chosen value
selected[i] = true;
state.add(choice);
// Proceed to the next round of selection
// Proceed to the next choice
backtrack(state, choices, selected, res);
// Backtrack: undo choice, restore to previous state
// Backtrack: undo the choice and restore the previous state
selected[i] = false;
state.remove(state.size() - 1);
}
@@ -48,6 +48,6 @@ public class permutations_ii {
List<List<Integer>> res = permutationsII(nums);
System.out.println("Input array nums = " + Arrays.toString(nums));
System.out.println("All permutations res = " + res);
System.out.println("All permutations: res = " + res);
}
}

View File

@@ -12,13 +12,13 @@ import java.util.*;
public class preorder_traversal_i_compact {
static List<TreeNode> res;
/* Preorder traversal: Example 1 */
/* Preorder traversal: example 1 */
static void preOrder(TreeNode root) {
if (root == null) {
return;
}
if (root.val == 7) {
// Record solution
// Record the solution
res.add(root);
}
preOrder(root.left);
@@ -27,7 +27,7 @@ public class preorder_traversal_i_compact {
public static void main(String[] args) {
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
System.out.println("\nInitialize binary tree");
System.out.println("\nInitialize the binary tree");
PrintUtil.printTree(root);
// Preorder traversal

View File

@@ -13,15 +13,15 @@ public class preorder_traversal_ii_compact {
static List<TreeNode> path;
static List<List<TreeNode>> res;
/* Preorder traversal: Example 2 */
/* Preorder traversal: example 2 */
static void preOrder(TreeNode root) {
if (root == null) {
return;
}
// Attempt
// Try
path.add(root);
if (root.val == 7) {
// Record solution
// Record the solution
res.add(new ArrayList<>(path));
}
preOrder(root.left);
@@ -32,7 +32,7 @@ public class preorder_traversal_ii_compact {
public static void main(String[] args) {
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
System.out.println("\nInitialize binary tree");
System.out.println("\nInitialize the binary tree");
PrintUtil.printTree(root);
// Preorder traversal
@@ -40,7 +40,7 @@ public class preorder_traversal_ii_compact {
res = new ArrayList<>();
preOrder(root);
System.out.println("\nOutput all paths from root node to node 7");
System.out.println("\nOutput all paths from the root to nodes with value 7");
for (List<TreeNode> path : res) {
List<Integer> vals = new ArrayList<>();
for (TreeNode node : path) {

View File

@@ -13,16 +13,16 @@ public class preorder_traversal_iii_compact {
static List<TreeNode> path;
static List<List<TreeNode>> res;
/* Preorder traversal: Example 3 */
/* Preorder traversal: example 3 */
static void preOrder(TreeNode root) {
// Pruning
// Prune
if (root == null || root.val == 3) {
return;
}
// Attempt
// Try
path.add(root);
if (root.val == 7) {
// Record solution
// Record the solution
res.add(new ArrayList<>(path));
}
preOrder(root.left);
@@ -33,7 +33,7 @@ public class preorder_traversal_iii_compact {
public static void main(String[] args) {
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
System.out.println("\nInitialize binary tree");
System.out.println("\nInitialize the binary tree");
PrintUtil.printTree(root);
// Preorder traversal
@@ -41,7 +41,7 @@ public class preorder_traversal_iii_compact {
res = new ArrayList<>();
preOrder(root);
System.out.println("\nOutput all paths from root node to node 7, paths do not include nodes with value 3");
System.out.println("\nOutput all paths from the root to nodes with value 7 that contain no node with value 3");
for (List<TreeNode> path : res) {
List<Integer> vals = new ArrayList<>();
for (TreeNode node : path) {

View File

@@ -10,47 +10,47 @@ import utils.*;
import java.util.*;
public class preorder_traversal_iii_template {
/* Check if the current state is a solution */
/* Check whether the current state is a solution */
static boolean isSolution(List<TreeNode> state) {
return !state.isEmpty() && state.get(state.size() - 1).val == 7;
}
/* Record solution */
/* Record the solution */
static void recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
res.add(new ArrayList<>(state));
}
/* Check if the choice is valid under the current state */
/* Check whether the choice is valid in the current state */
static boolean isValid(List<TreeNode> state, TreeNode choice) {
return choice != null && choice.val != 3;
}
/* Update state */
/* Update the state */
static void makeChoice(List<TreeNode> state, TreeNode choice) {
state.add(choice);
}
/* Restore state */
/* Restore the state */
static void undoChoice(List<TreeNode> state, TreeNode choice) {
state.remove(state.size() - 1);
}
/* Backtracking algorithm: Example 3 */
/* Backtracking: example 3 */
static void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
// Check if it is a solution
// Check whether this is a solution
if (isSolution(state)) {
// Record solution
// Record the solution
recordSolution(state, res);
}
// Traverse all choices
// Iterate over all choices
for (TreeNode choice : choices) {
// Pruning: check if the choice is valid
// Prune: check whether the choice is valid
if (isValid(state, choice)) {
// Attempt: make choice, update state
// Try: make a choice and update the state
makeChoice(state, choice);
// Proceed to the next round of selection
// Proceed to the next choice
backtrack(state, Arrays.asList(choice.left, choice.right), res);
// Backtrack: undo choice, restore to previous state
// Backtrack: undo the choice and restore the previous state
undoChoice(state, choice);
}
}
@@ -58,14 +58,14 @@ public class preorder_traversal_iii_template {
public static void main(String[] args) {
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
System.out.println("\nInitialize binary tree");
System.out.println("\nInitialize the binary tree");
PrintUtil.printTree(root);
// Backtracking algorithm
List<List<TreeNode>> res = new ArrayList<>();
backtrack(new ArrayList<>(), Arrays.asList(root), res);
System.out.println("\nOutput all paths from root node to node 7, requiring paths do not include nodes with value 3");
System.out.println("\nOutput all paths from the root to nodes with value 7, requiring that no node on the path has value 3");
for (List<TreeNode> path : res) {
List<Integer> vals = new ArrayList<>();
for (TreeNode node : path) {

View File

@@ -9,26 +9,26 @@ package chapter_backtracking;
import java.util.*;
public class subset_sum_i {
/* Backtracking algorithm: Subset sum I */
/* Backtracking: subset sum I */
static void backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {
// When the subset sum equals target, record the solution
// Record a solution when the remaining target is zero, so the subset sums to the original target
if (target == 0) {
res.add(new ArrayList<>(state));
return;
}
// Traverse all choices
// Pruning 2: start traversing from start to avoid generating duplicate subsets
// Iterate over all choices
// Pruning 2: iterate from start to avoid generating duplicate subsets
for (int i = start; i < choices.length; i++) {
// Pruning 1: if the subset sum exceeds target, end the loop directly
// This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target
// Pruning 1: exit the loop if the next element exceeds the remaining target
// The array is sorted, so later elements are at least as large and also exceed the remaining target
if (target - choices[i] < 0) {
break;
}
// Attempt: make choice, update target, start
// Try: make a choice and update target and start
state.add(choices[i]);
// Proceed to the next round of selection
// Proceed to the next choice
backtrack(state, target - choices[i], choices, i, res);
// Backtrack: undo choice, restore to previous state
// Backtrack: undo the choice and restore the previous state
state.remove(state.size() - 1);
}
}
@@ -37,8 +37,8 @@ public class subset_sum_i {
static List<List<Integer>> subsetSumI(int[] nums, int target) {
List<Integer> state = new ArrayList<>(); // State (subset)
Arrays.sort(nums); // Sort nums
int start = 0; // Start point for traversal
List<List<Integer>> res = new ArrayList<>(); // Result list (subset list)
int start = 0; // Starting index for iteration
List<List<Integer>> res = new ArrayList<>(); // Result list (list of subsets)
backtrack(state, target, nums, start, res);
return res;
}
@@ -50,6 +50,6 @@ public class subset_sum_i {
List<List<Integer>> res = subsetSumI(nums, target);
System.out.println("Input array nums = " + Arrays.toString(nums) + ", target = " + target);
System.out.println("All subsets with sum equal to " + target + " are res = " + res);
System.out.println("All subsets with sum " + target + " : res = " + res);
}
}

View File

@@ -9,33 +9,33 @@ package chapter_backtracking;
import java.util.*;
public class subset_sum_i_naive {
/* Backtracking algorithm: Subset sum I */
/* Backtracking: subset sum I */
static void backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {
// When the subset sum equals target, record the solution
// Record a solution when the subset sum equals target
if (total == target) {
res.add(new ArrayList<>(state));
return;
}
// Traverse all choices
// Iterate over all choices
for (int i = 0; i < choices.length; i++) {
// Pruning: if the subset sum exceeds target, skip this choice
// Prune: skip this choice if the subset sum would exceed target
if (total + choices[i] > target) {
continue;
}
// Attempt: make choice, update element sum total
// Try: make a choice and update the sum total
state.add(choices[i]);
// Proceed to the next round of selection
// Proceed to the next choice
backtrack(state, target, total + choices[i], choices, res);
// Backtrack: undo choice, restore to previous state
// Backtrack: undo the choice and restore the previous state
state.remove(state.size() - 1);
}
}
/* Solve subset sum I (including duplicate subsets) */
/* Solve subset sum I (with duplicate subsets) */
static List<List<Integer>> subsetSumINaive(int[] nums, int target) {
List<Integer> state = new ArrayList<>(); // State (subset)
int total = 0; // Subset sum
List<List<Integer>> res = new ArrayList<>(); // Result list (subset list)
List<List<Integer>> res = new ArrayList<>(); // Result list (list of subsets)
backtrack(state, target, total, nums, res);
return res;
}
@@ -47,7 +47,7 @@ public class subset_sum_i_naive {
List<List<Integer>> res = subsetSumINaive(nums, target);
System.out.println("Input array nums = " + Arrays.toString(nums) + ", target = " + target);
System.out.println("All subsets with sum equal to " + target + " are res = " + res);
System.out.println("Please note that this method outputs results containing duplicate sets");
System.out.println("All subsets with sum " + target + " : res = " + res);
System.out.println("Note that this method produces duplicate subsets");
}
}

View File

@@ -9,31 +9,31 @@ package chapter_backtracking;
import java.util.*;
public class subset_sum_ii {
/* Backtracking algorithm: Subset sum II */
/* Backtracking: subset sum II */
static void backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {
// When the subset sum equals target, record the solution
// Record a solution when the remaining target is zero, so the subset sums to the original target
if (target == 0) {
res.add(new ArrayList<>(state));
return;
}
// Traverse all choices
// Pruning 2: start traversing from start to avoid generating duplicate subsets
// Pruning 3: start traversing from start to avoid repeatedly selecting the same element
// Iterate over all choices
// Pruning 2: iterate from start to avoid generating duplicate subsets
// Pruning 3: iterate from start to avoid choosing the same element again
for (int i = start; i < choices.length; i++) {
// Pruning 1: if the subset sum exceeds target, end the loop directly
// This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target
// Pruning 1: exit the loop if the next element exceeds the remaining target
// The array is sorted, so later elements are at least as large and also exceed the remaining target
if (target - choices[i] < 0) {
break;
}
// Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly
// Pruning 4: if this element equals its left neighbor at the same search depth, the branch is a duplicate; skip it
if (i > start && choices[i] == choices[i - 1]) {
continue;
}
// Attempt: make choice, update target, start
// Try: make a choice and update target and start
state.add(choices[i]);
// Proceed to the next round of selection
// Proceed to the next choice
backtrack(state, target - choices[i], choices, i + 1, res);
// Backtrack: undo choice, restore to previous state
// Backtrack: undo the choice and restore the previous state
state.remove(state.size() - 1);
}
}
@@ -42,8 +42,8 @@ public class subset_sum_ii {
static List<List<Integer>> subsetSumII(int[] nums, int target) {
List<Integer> state = new ArrayList<>(); // State (subset)
Arrays.sort(nums); // Sort nums
int start = 0; // Start point for traversal
List<List<Integer>> res = new ArrayList<>(); // Result list (subset list)
int start = 0; // Starting index for iteration
List<List<Integer>> res = new ArrayList<>(); // Result list (list of subsets)
backtrack(state, target, nums, start, res);
return res;
}
@@ -55,6 +55,6 @@ public class subset_sum_ii {
List<List<Integer>> res = subsetSumII(nums, target);
System.out.println("Input array nums = " + Arrays.toString(nums) + ", target = " + target);
System.out.println("All subsets with sum equal to " + target + " are res = " + res);
System.out.println("All subsets with sum " + target + " : res = " + res);
}
}