Translate all code to English (#1836)

* Review the EN heading format.

* Fix pythontutor headings.

* Fix pythontutor headings.

* bug fixes

* Fix headings in **/summary.md

* Revisit the CN-to-EN translation for Python code using Claude-4.5

* Revisit the CN-to-EN translation for Java code using Claude-4.5

* Revisit the CN-to-EN translation for Cpp code using Claude-4.5.

* Fix the dictionary.

* Fix cpp code translation for the multipart strings.

* Translate Go code to English.

* Update workflows to test EN code.

* Add EN translation for C.

* Add EN translation for CSharp.

* Add EN translation for Swift.

* Trigger the CI check.

* Revert.

* Update en/hash_map.md

* Add the EN version of Dart code.

* Add the EN version of Kotlin code.

* Add missing code files.

* Add the EN version of JavaScript code.

* Add the EN version of TypeScript code.

* Fix the workflows.

* Add the EN version of Ruby code.

* Add the EN version of Rust code.

* Update the CI check for the English version  code.

* Update Python CI check.

* Fix cmakelists for en/C code.

* Fix Ruby comments
This commit is contained in:
Yudong Jin
2025-12-31 07:44:52 +08:00
committed by GitHub
parent 45e1295241
commit 2778a6f9c7
1284 changed files with 71557 additions and 3275 deletions
@@ -0,0 +1,75 @@
/**
* File: n_queens.dart
* Created Time: 2023-08-10
* Author: liuyuxin (gvenusleo@gmail.com)
*/
/* Backtracking algorithm: N queens */
void backtrack(
int row,
int n,
List<List<String>> state,
List<List<List<String>>> res,
List<bool> cols,
List<bool> diags1,
List<bool> diags2,
) {
// When all rows are placed, record the solution
if (row == n) {
List<List<String>> copyState = [];
for (List<String> sRow in state) {
copyState.add(List.from(sRow));
}
res.add(copyState);
return;
}
// Traverse all columns
for (int col = 0; col < n; col++) {
// Calculate the main diagonal and anti-diagonal corresponding to this cell
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
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// Attempt: place the queen in this cell
state[row][col] = "Q";
cols[col] = true;
diags1[diag1] = true;
diags2[diag2] = true;
// Place the next row
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// Backtrack: restore this cell to an empty cell
state[row][col] = "#";
cols[col] = false;
diags1[diag1] = false;
diags2[diag2] = false;
}
}
}
/* Solve N queens */
List<List<List<String>>> nQueens(int n) {
// Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell
List<List<String>> state = List.generate(n, (index) => List.filled(n, "#"));
List<bool> cols = List.filled(n, false); // Record whether there is a queen in the column
List<bool> diags1 = List.filled(2 * n - 1, false); // Record whether there is a queen on the main diagonal
List<bool> diags2 = List.filled(2 * n - 1, false); // Record whether there is a queen on the anti-diagonal
List<List<List<String>>> res = [];
backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
/* Driver Code */
void main() {
int n = 4;
List<List<List<String>>> res = nQueens(n);
print("Input board size is $n");
print("Total queen placement solutions: ${res.length}");
for (List<List<String>> state in res) {
print("--------------------");
for (List<String> row in state) {
print(row);
}
}
}
@@ -0,0 +1,51 @@
/**
* File: permutations_i.dart
* Created Time: 2023-08-10
* Author: liuyuxin (gvenusleo@gmail.com)
*/
/* Backtracking algorithm: Permutations I */
void backtrack(
List<int> state,
List<int> choices,
List<bool> selected,
List<List<int>> res,
) {
// When the state length equals the number of elements, record the solution
if (state.length == choices.length) {
res.add(List.from(state));
return;
}
// Traverse all choices
for (int i = 0; i < choices.length; i++) {
int choice = choices[i];
// Pruning: do not allow repeated selection of elements
if (!selected[i]) {
// Attempt: make choice, update state
selected[i] = true;
state.add(choice);
// Proceed to the next round of selection
backtrack(state, choices, selected, res);
// Backtrack: undo choice, restore to previous state
selected[i] = false;
state.removeLast();
}
}
}
/* Permutations I */
List<List<int>> permutationsI(List<int> nums) {
List<List<int>> res = [];
backtrack([], nums, List.filled(nums.length, false), res);
return res;
}
/* Driver Code */
void main() {
List<int> nums = [1, 2, 3];
List<List<int>> res = permutationsI(nums);
print("Input array nums = $nums");
print("All permutations res = $res");
}
@@ -0,0 +1,53 @@
/**
* File: permutations_ii.dart
* Created Time: 2023-08-10
* Author: liuyuxin (gvenusleo@gmail.com)
*/
/* Backtracking algorithm: Permutations II */
void backtrack(
List<int> state,
List<int> choices,
List<bool> selected,
List<List<int>> res,
) {
// When the state length equals the number of elements, record the solution
if (state.length == choices.length) {
res.add(List.from(state));
return;
}
// Traverse all choices
Set<int> duplicated = {};
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
if (!selected[i] && !duplicated.contains(choice)) {
// Attempt: make choice, update state
duplicated.add(choice); // Record the selected element value
selected[i] = true;
state.add(choice);
// Proceed to the next round of selection
backtrack(state, choices, selected, res);
// Backtrack: undo choice, restore to previous state
selected[i] = false;
state.removeLast();
}
}
}
/* Permutations II */
List<List<int>> permutationsII(List<int> nums) {
List<List<int>> res = [];
backtrack([], nums, List.filled(nums.length, false), res);
return res;
}
/* Driver Code */
void main() {
List<int> nums = [1, 2, 2];
List<List<int>> res = permutationsII(nums);
print("Input array nums = $nums");
print("All permutations res = $res");
}
@@ -0,0 +1,35 @@
/**
* File: preorder_traversal_i_compact.dart
* Created Time: 2023-08-10
* Author: liuyuxin (gvenusleo@gmail.com)
*/
import '../utils/print_util.dart';
import '../utils/tree_node.dart';
/* Preorder traversal: Example 1 */
void preOrder(TreeNode? root, List<TreeNode> res) {
if (root == null) {
return;
}
if (root.val == 7) {
// Record solution
res.add(root);
}
preOrder(root.left, res);
preOrder(root.right, res);
}
/* Driver Code */
void main() {
TreeNode? root = listToTree([1, 7, 3, 4, 5, 6, 7]);
print("\nInitialize binary tree");
printTree(root);
// Preorder traversal
List<TreeNode> res = [];
preOrder(root, res);
print("\nOutput all nodes with value 7");
print(List.generate(res.length, (i) => res[i].val));
}
@@ -0,0 +1,47 @@
/**
* File: preorder_traversal_ii_compact.dart
* Created Time: 2023-08-10
* Author: liuyuxin (gvenusleo@gmail.com)
*/
import '../utils/print_util.dart';
import '../utils/tree_node.dart';
/* Preorder traversal: Example 2 */
void preOrder(
TreeNode? root,
List<TreeNode> path,
List<List<TreeNode>> res,
) {
if (root == null) {
return;
}
// Attempt
path.add(root);
if (root.val == 7) {
// Record solution
res.add(List.from(path));
}
preOrder(root.left, path, res);
preOrder(root.right, path, res);
// Backtrack
path.removeLast();
}
/* Driver Code */
void main() {
TreeNode? root = listToTree([1, 7, 3, 4, 5, 6, 7]);
print("\nInitialize binary tree");
printTree(root);
// Preorder traversal
List<TreeNode> path = [];
List<List<TreeNode>> res = [];
preOrder(root, path, res);
print("\nOutput all paths from root node to node 7");
for (List<TreeNode> vals in res) {
print(List.generate(vals.length, (i) => vals[i].val));
}
}
@@ -0,0 +1,47 @@
/**
* File: preorder_traversal_iii_compact.dart
* Created Time: 2023-08-10
* Author: liuyuxin (gvenusleo@gmail.com)
*/
import '../utils/print_util.dart';
import '../utils/tree_node.dart';
/* Preorder traversal: Example 3 */
void preOrder(
TreeNode? root,
List<TreeNode> path,
List<List<TreeNode>> res,
) {
if (root == null || root.val == 3) {
return;
}
// Attempt
path.add(root);
if (root.val == 7) {
// Record solution
res.add(List.from(path));
}
preOrder(root.left, path, res);
preOrder(root.right, path, res);
// Backtrack
path.removeLast();
}
/* Driver Code */
void main() {
TreeNode? root = listToTree([1, 7, 3, 4, 5, 6, 7]);
print("\nInitialize binary tree");
printTree(root);
// Preorder traversal
List<TreeNode> path = [];
List<List<TreeNode>> res = [];
preOrder(root, path, res);
print("\nOutput all paths from root node to node 7");
for (List<TreeNode> vals in res) {
print(List.generate(vals.length, (i) => vals[i].val));
}
}
@@ -0,0 +1,73 @@
/**
* File: preorder_traversal_iii_template.dart
* Created Time: 2023-08-10
* Author: liuyuxin (gvenusleo@gmail.com)
*/
import '../utils/print_util.dart';
import '../utils/tree_node.dart';
/* Check if the current state is a solution */
bool isSolution(List<TreeNode> state) {
return state.isNotEmpty && state.last.val == 7;
}
/* Record solution */
void recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
res.add(List.from(state));
}
/* Check if the choice is valid under the current state */
bool isValid(List<TreeNode> state, TreeNode? choice) {
return choice != null && choice.val != 3;
}
/* Update state */
void makeChoice(List<TreeNode> state, TreeNode? choice) {
state.add(choice!);
}
/* Restore state */
void undoChoice(List<TreeNode> state, TreeNode? choice) {
state.removeLast();
}
/* Backtracking algorithm: Example 3 */
void backtrack(
List<TreeNode> state,
List<TreeNode?> choices,
List<List<TreeNode>> res,
) {
// Check if it is a solution
if (isSolution(state)) {
// Record solution
recordSolution(state, res);
}
// Traverse all choices
for (TreeNode? choice in choices) {
// Pruning: check if the choice is valid
if (isValid(state, choice)) {
// Attempt: make choice, update state
makeChoice(state, choice);
// Proceed to the next round of selection
backtrack(state, [choice!.left, choice.right], res);
// Backtrack: undo choice, restore to previous state
undoChoice(state, choice);
}
}
}
/* Driver Code */
void main() {
TreeNode? root = listToTree([1, 7, 3, 4, 5, 6, 7]);
print("\nInitialize binary tree");
printTree(root);
// Backtracking algorithm
List<List<TreeNode>> res = [];
backtrack([], [root!], res);
print("\nOutput all paths from root node to node 7, requiring paths do not include nodes with value 3");
for (List<TreeNode> path in res) {
print(List.from(path.map((e) => e.val)));
}
}
@@ -0,0 +1,56 @@
/**
* File: subset_sum_i.dart
* Created Time: 2023-08-10
* Author: liuyuxin (gvenusleo@gmail.com)
*/
/* Backtracking algorithm: Subset sum I */
void backtrack(
List<int> state,
int target,
List<int> choices,
int start,
List<List<int>> res,
) {
// When the subset sum equals target, record the solution
if (target == 0) {
res.add(List.from(state));
return;
}
// Traverse all choices
// Pruning 2: start traversing 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
if (target - choices[i] < 0) {
break;
}
// Attempt: make choice, update target, start
state.add(choices[i]);
// Proceed to the next round of selection
backtrack(state, target - choices[i], choices, i, res);
// Backtrack: undo choice, restore to previous state
state.removeLast();
}
}
/* Solve subset sum I */
List<List<int>> subsetSumI(List<int> nums, int target) {
List<int> state = []; // State (subset)
nums.sort(); // Sort nums
int start = 0; // Start point for traversal
List<List<int>> res = []; // Result list (subset list)
backtrack(state, target, nums, start, res);
return res;
}
/* Driver Code */
void main() {
List<int> nums = [3, 4, 5];
int target = 9;
List<List<int>> res = subsetSumI(nums, target);
print("Input array nums = $nums, target = $target");
print("All subsets with sum equal to $target res = $res");
}
@@ -0,0 +1,54 @@
/**
* File: subset_sum_i_naive.dart
* Created Time: 2023-08-10
* Author: liuyuxin (gvenusleo@gmail.com)
*/
/* Backtracking algorithm: Subset sum I */
void backtrack(
List<int> state,
int target,
int total,
List<int> choices,
List<List<int>> res,
) {
// When the subset sum equals target, record the solution
if (total == target) {
res.add(List.from(state));
return;
}
// Traverse all choices
for (int i = 0; i < choices.length; i++) {
// Pruning: if the subset sum exceeds target, skip this choice
if (total + choices[i] > target) {
continue;
}
// Attempt: make choice, update element sum total
state.add(choices[i]);
// Proceed to the next round of selection
backtrack(state, target, total + choices[i], choices, res);
// Backtrack: undo choice, restore to previous state
state.removeLast();
}
}
/* Solve subset sum I (including duplicate subsets) */
List<List<int>> subsetSumINaive(List<int> nums, int target) {
List<int> state = []; // State (subset)
int total = 0; // Sum of elements
List<List<int>> res = []; // Result list (subset list)
backtrack(state, target, total, nums, res);
return res;
}
/* Driver Code */
void main() {
List<int> nums = [3, 4, 5];
int target = 9;
List<List<int>> res = subsetSumINaive(nums, target);
print("Input array nums = $nums, target = $target");
print("All subsets with sum equal to $target res = $res");
print("Please note that this method outputs results containing duplicate sets");
}
@@ -0,0 +1,61 @@
/**
* File: subset_sum_ii.dart
* Created Time: 2023-08-10
* Author: liuyuxin (gvenusleo@gmail.com)
*/
/* Backtracking algorithm: Subset sum II */
void backtrack(
List<int> state,
int target,
List<int> choices,
int start,
List<List<int>> res,
) {
// When the subset sum equals target, record the solution
if (target == 0) {
res.add(List.from(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
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
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
if (i > start && choices[i] == choices[i - 1]) {
continue;
}
// Attempt: make choice, update target, start
state.add(choices[i]);
// Proceed to the next round of selection
backtrack(state, target - choices[i], choices, i + 1, res);
// Backtrack: undo choice, restore to previous state
state.removeLast();
}
}
/* Solve subset sum II */
List<List<int>> subsetSumII(List<int> nums, int target) {
List<int> state = []; // State (subset)
nums.sort(); // Sort nums
int start = 0; // Start point for traversal
List<List<int>> res = []; // Result list (subset list)
backtrack(state, target, nums, start, res);
return res;
}
/* Driver Code */
void main() {
List<int> nums = [4, 4, 5];
int target = 9;
List<List<int>> res = subsetSumII(nums, target);
print("Input array nums = $nums, target = $target");
print("All subsets with sum equal to $target res = $res");
}