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,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);
}
}