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,76 @@
/*
* File: n_queens.rs
* Created Time: 2023-07-15
* Author: codingonion (coderonion@gmail.com)
*/
/* Backtracking algorithm: N queens */
fn backtrack(
row: usize,
n: usize,
state: &mut Vec<Vec<String>>,
res: &mut Vec<Vec<Vec<String>>>,
cols: &mut [bool],
diags1: &mut [bool],
diags2: &mut [bool],
) {
// When all rows are placed, record the solution
if row == n {
res.push(state.clone());
return;
}
// Traverse all columns
for col in 0..n {
// Calculate the main diagonal and anti-diagonal corresponding to this cell
let diag1 = row + n - 1 - col;
let 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".into();
(cols[col], diags1[diag1], diags2[diag2]) = (true, true, 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] = "#".into();
(cols[col], diags1[diag1], diags2[diag2]) = (false, false, false);
}
}
}
/* Solve N queens */
fn n_queens(n: usize) -> Vec<Vec<Vec<String>>> {
// Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell
let mut state: Vec<Vec<String>> = vec![vec!["#".to_string(); n]; n];
let mut cols = vec![false; n]; // Record whether there is a queen in the column
let mut diags1 = vec![false; 2 * n - 1]; // Record whether there is a queen on the main diagonal
let mut diags2 = vec![false; 2 * n - 1]; // Record whether there is a queen on the anti-diagonal
let mut res: Vec<Vec<Vec<String>>> = Vec::new();
backtrack(
0,
n,
&mut state,
&mut res,
&mut cols,
&mut diags1,
&mut diags2,
);
res
}
/* Driver Code */
pub fn main() {
let n: usize = 4;
let res = n_queens(n);
println!("Input board size is {n}");
println!("Total queen placement solutions: {}", res.len());
for state in res.iter() {
println!("--------------------");
for row in state.iter() {
println!("{:?}", row);
}
}
}
@@ -0,0 +1,46 @@
/*
* File: permutations_i.rs
* Created Time: 2023-07-15
* Author: codingonion (coderonion@gmail.com)
*/
/* Backtracking algorithm: Permutations I */
fn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {
// When the state length equals the number of elements, record the solution
if state.len() == choices.len() {
res.push(state);
return;
}
// Traverse all choices
for i in 0..choices.len() {
let choice = choices[i];
// Pruning: do not allow repeated selection of elements
if !selected[i] {
// Attempt: make choice, update state
selected[i] = true;
state.push(choice);
// Proceed to the next round of selection
backtrack(state.clone(), choices, selected, res);
// Backtrack: undo choice, restore to previous state
selected[i] = false;
state.pop();
}
}
}
/* Permutations I */
fn permutations_i(nums: &mut [i32]) -> Vec<Vec<i32>> {
let mut res = Vec::new(); // State (subset)
backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);
res
}
/* Driver Code */
pub fn main() {
let mut nums = [1, 2, 3];
let res = permutations_i(&mut nums);
println!("Input array nums = {:?}", &nums);
println!("All permutations res = {:?}", &res);
}
@@ -0,0 +1,50 @@
/*
* File: permutations_ii.rs
* Created Time: 2023-07-15
* Author: codingonion (coderonion@gmail.com)
*/
use std::collections::HashSet;
/* Backtracking algorithm: Permutations II */
fn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {
// When the state length equals the number of elements, record the solution
if state.len() == choices.len() {
res.push(state);
return;
}
// Traverse all choices
let mut duplicated = HashSet::<i32>::new();
for i in 0..choices.len() {
let 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.insert(choice); // Record the selected element value
selected[i] = true;
state.push(choice);
// Proceed to the next round of selection
backtrack(state.clone(), choices, selected, res);
// Backtrack: undo choice, restore to previous state
selected[i] = false;
state.pop();
}
}
}
/* Permutations II */
fn permutations_ii(nums: &mut [i32]) -> Vec<Vec<i32>> {
let mut res = Vec::new();
backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);
res
}
/* Driver Code */
pub fn main() {
let mut nums = [1, 2, 2];
let res = permutations_ii(&mut nums);
println!("Input array nums = {:?}", &nums);
println!("All permutations res = {:?}", &res);
}
@@ -0,0 +1,41 @@
/*
* File: preorder_traversal_i_compact.rs
* Created Time: 2023-07-15
* Author: codingonion (coderonion@gmail.com)
*/
use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode};
use std::{cell::RefCell, rc::Rc};
/* Preorder traversal: Example 1 */
fn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<&Rc<RefCell<TreeNode>>>) {
if root.is_none() {
return;
}
if let Some(node) = root {
if node.borrow().val == 7 {
// Record solution
res.push(node.clone());
}
pre_order(res, node.borrow().left.as_ref());
pre_order(res, node.borrow().right.as_ref());
}
}
/* Driver Code */
pub fn main() {
let root = vec_to_tree([1, 7, 3, 4, 5, 6, 7].map(|x| Some(x)).to_vec());
println!("Initialize binary tree");
print_util::print_tree(root.as_ref().unwrap());
// Preorder traversal
let mut res = Vec::new();
pre_order(&mut res, root.as_ref());
println!("\nOutput all nodes with value 7");
let mut vals = Vec::new();
for node in res {
vals.push(node.borrow().val)
}
println!("{:?}", vals);
}
@@ -0,0 +1,52 @@
/*
* File: preorder_traversal_ii_compact.rs
* Created Time: 2023-07-15
* Author: codingonion (coderonion@gmail.com)
*/
use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode};
use std::{cell::RefCell, rc::Rc};
/* Preorder traversal: Example 2 */
fn pre_order(
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
path: &mut Vec<Rc<RefCell<TreeNode>>>,
root: Option<&Rc<RefCell<TreeNode>>>,
) {
if root.is_none() {
return;
}
if let Some(node) = root {
// Attempt
path.push(node.clone());
if node.borrow().val == 7 {
// Record solution
res.push(path.clone());
}
pre_order(res, path, node.borrow().left.as_ref());
pre_order(res, path, node.borrow().right.as_ref());
// Backtrack
path.pop();
}
}
/* Driver Code */
pub fn main() {
let root = vec_to_tree([1, 7, 3, 4, 5, 6, 7].map(|x| Some(x)).to_vec());
println!("Initialize binary tree");
print_util::print_tree(root.as_ref().unwrap());
// Preorder traversal
let mut path = Vec::new();
let mut res = Vec::new();
pre_order(&mut res, &mut path, root.as_ref());
println!("\nOutput all paths from root node to node 7");
for path in res {
let mut vals = Vec::new();
for node in path {
vals.push(node.borrow().val)
}
println!("{:?}", vals);
}
}
@@ -0,0 +1,53 @@
/*
* File: preorder_traversal_iii_compact.rs
* Created Time: 2023-07-15
* Author: codingonion (coderonion@gmail.com)
*/
use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode};
use std::{cell::RefCell, rc::Rc};
/* Preorder traversal: Example 3 */
fn pre_order(
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
path: &mut Vec<Rc<RefCell<TreeNode>>>,
root: Option<&Rc<RefCell<TreeNode>>>,
) {
// Pruning
if root.is_none() || root.as_ref().unwrap().borrow().val == 3 {
return;
}
if let Some(node) = root {
// Attempt
path.push(node.clone());
if node.borrow().val == 7 {
// Record solution
res.push(path.clone());
}
pre_order(res, path, node.borrow().left.as_ref());
pre_order(res, path, node.borrow().right.as_ref());
// Backtrack
path.pop();
}
}
/* Driver Code */
pub fn main() {
let root = vec_to_tree([1, 7, 3, 4, 5, 6, 7].map(|x| Some(x)).to_vec());
println!("Initialize binary tree");
print_util::print_tree(root.as_ref().unwrap());
// Preorder traversal
let mut path = Vec::new();
let mut res = Vec::new();
pre_order(&mut res, &mut path, root.as_ref());
println!("\nOutput all paths from root node to node 7, paths do not include nodes with value 3");
for path in res {
let mut vals = Vec::new();
for node in path {
vals.push(node.borrow().val)
}
println!("{:?}", vals);
}
}
@@ -0,0 +1,88 @@
/*
* File: preorder_traversal_iii_template.rs
* Created Time: 2023-07-15
* Author: codingonion (coderonion@gmail.com)
*/
use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode};
use std::{cell::RefCell, rc::Rc};
/* Check if the current state is a solution */
fn is_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>) -> bool {
return !state.is_empty() && state.last().unwrap().borrow().val == 7;
}
/* Record solution */
fn record_solution(
state: &mut Vec<Rc<RefCell<TreeNode>>>,
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
) {
res.push(state.clone());
}
/* Check if the choice is valid under the current state */
fn is_valid(_: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Option<&Rc<RefCell<TreeNode>>>) -> bool {
return choice.is_some() && choice.unwrap().borrow().val != 3;
}
/* Update state */
fn make_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) {
state.push(choice);
}
/* Restore state */
fn undo_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, _: Rc<RefCell<TreeNode>>) {
state.pop();
}
/* Backtracking algorithm: Example 3 */
fn backtrack(
state: &mut Vec<Rc<RefCell<TreeNode>>>,
choices: &Vec<Option<&Rc<RefCell<TreeNode>>>>,
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
) {
// Check if it is a solution
if is_solution(state) {
// Record solution
record_solution(state, res);
}
// Traverse all choices
for &choice in choices.iter() {
// Pruning: check if the choice is valid
if is_valid(state, choice) {
// Attempt: make choice, update state
make_choice(state, choice.unwrap().clone());
// Proceed to the next round of selection
backtrack(
state,
&vec![
choice.unwrap().borrow().left.as_ref(),
choice.unwrap().borrow().right.as_ref(),
],
res,
);
// Backtrack: undo choice, restore to previous state
undo_choice(state, choice.unwrap().clone());
}
}
}
/* Driver Code */
pub fn main() {
let root = vec_to_tree([1, 7, 3, 4, 5, 6, 7].map(|x| Some(x)).to_vec());
println!("Initialize binary tree");
print_util::print_tree(root.as_ref().unwrap());
// Backtracking algorithm
let mut res = Vec::new();
backtrack(&mut Vec::new(), &mut vec![root.as_ref()], &mut res);
println!("\nOutput all paths from root node to node 7, requiring paths do not include nodes with value 3");
for path in res {
let mut vals = Vec::new();
for node in path {
vals.push(node.borrow().val)
}
println!("{:?}", vals);
}
}
@@ -0,0 +1,56 @@
/*
* File: subset_sum_i.rs
* Created Time: 2023-07-09
* Author: codingonion (coderonion@gmail.com)
*/
/* Backtracking algorithm: Subset sum I */
fn backtrack(
state: &mut Vec<i32>,
target: i32,
choices: &[i32],
start: usize,
res: &mut Vec<Vec<i32>>,
) {
// When the subset sum equals target, record the solution
if target == 0 {
res.push(state.clone());
return;
}
// Traverse all choices
// Pruning 2: start traversing from start to avoid generating duplicate subsets
for i in start..choices.len() {
// 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.push(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.pop();
}
}
/* Solve subset sum I */
fn subset_sum_i(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {
let mut state = Vec::new(); // State (subset)
nums.sort(); // Sort nums
let start = 0; // Start point for traversal
let mut res = Vec::new(); // Result list (subset list)
backtrack(&mut state, target, nums, start, &mut res);
res
}
/* Driver Code */
pub fn main() {
let mut nums = [3, 4, 5];
let target = 9;
let res = subset_sum_i(&mut nums, target);
println!("Input array nums = {:?}, target = {}", &nums, target);
println!("All subsets with sum equal to {} res = {:?}", target, &res);
}
@@ -0,0 +1,54 @@
/*
* File: subset_sum_i_naive.rs
* Created Time: 2023-07-09
* Author: codingonion (coderonion@gmail.com)
*/
/* Backtracking algorithm: Subset sum I */
fn backtrack(
state: &mut Vec<i32>,
target: i32,
total: i32,
choices: &[i32],
res: &mut Vec<Vec<i32>>,
) {
// When the subset sum equals target, record the solution
if total == target {
res.push(state.clone());
return;
}
// Traverse all choices
for i in 0..choices.len() {
// Pruning: if the subset sum exceeds target, skip this choice
if total + choices[i] > target {
continue;
}
// Attempt: make choice, update element sum total
state.push(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.pop();
}
}
/* Solve subset sum I (including duplicate subsets) */
fn subset_sum_i_naive(nums: &[i32], target: i32) -> Vec<Vec<i32>> {
let mut state = Vec::new(); // State (subset)
let total = 0; // Subset sum
let mut res = Vec::new(); // Result list (subset list)
backtrack(&mut state, target, total, nums, &mut res);
res
}
/* Driver Code */
pub fn main() {
let nums = [3, 4, 5];
let target = 9;
let res = subset_sum_i_naive(&nums, target);
println!("Input array nums = {:?}, target = {}", &nums, target);
println!("All subsets with sum equal to {} res = {:?}", target, &res);
println!("Please note that this method outputs results containing duplicate sets");
}
@@ -0,0 +1,61 @@
/*
* File: subset_sum_ii.rs
* Created Time: 2023-07-09
* Author: codingonion (coderonion@gmail.com)
*/
/* Backtracking algorithm: Subset sum II */
fn backtrack(
state: &mut Vec<i32>,
target: i32,
choices: &[i32],
start: usize,
res: &mut Vec<Vec<i32>>,
) {
// When the subset sum equals target, record the solution
if target == 0 {
res.push(state.clone());
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 i in start..choices.len() {
// 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.push(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.pop();
}
}
/* Solve subset sum II */
fn subset_sum_ii(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {
let mut state = Vec::new(); // State (subset)
nums.sort(); // Sort nums
let start = 0; // Start point for traversal
let mut res = Vec::new(); // Result list (subset list)
backtrack(&mut state, target, nums, start, &mut res);
res
}
/* Driver Code */
pub fn main() {
let mut nums = [4, 4, 5];
let target = 9;
let res = subset_sum_ii(&mut nums, target);
println!("Input array nums = {:?}, target = {}", &nums, target);
println!("All subsets with sum equal to {} res = {:?}", target, &res);
}