mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-16 08:56:05 +00:00
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:
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* File: n_queens.cs
|
||||
* Created Time: 2023-05-04
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class n_queens {
|
||||
/* Backtracking algorithm: N queens */
|
||||
void Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,
|
||||
bool[] cols, bool[] diags1, bool[] diags2) {
|
||||
// When all rows are placed, record the solution
|
||||
if (row == n) {
|
||||
List<List<string>> copyState = [];
|
||||
foreach (List<string> sRow in state) {
|
||||
copyState.Add(new List<string>(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] = diags1[diag1] = 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] = diags1[diag1] = 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 = [];
|
||||
for (int i = 0; i < n; i++) {
|
||||
List<string> row = [];
|
||||
for (int j = 0; j < n; j++) {
|
||||
row.Add("#");
|
||||
}
|
||||
state.Add(row);
|
||||
}
|
||||
bool[] cols = new bool[n]; // Record whether there is a queen in the column
|
||||
bool[] diags1 = new bool[2 * n - 1]; // Record whether there is a queen on the main diagonal
|
||||
bool[] diags2 = new bool[2 * n - 1]; // 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;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 4;
|
||||
List<List<List<string>>> res = NQueens(n);
|
||||
|
||||
Console.WriteLine("Input board size is " + n);
|
||||
Console.WriteLine("Total queen placement solutions: " + res.Count + " solutions");
|
||||
foreach (List<List<string>> state in res) {
|
||||
Console.WriteLine("--------------------");
|
||||
foreach (List<string> row in state) {
|
||||
PrintUtil.PrintList(row);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* File: permutations_i.cs
|
||||
* Created Time: 2023-04-24
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class permutations_i {
|
||||
/* Backtracking algorithm: Permutations I */
|
||||
void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
|
||||
// When the state length equals the number of elements, record the solution
|
||||
if (state.Count == choices.Length) {
|
||||
res.Add(new List<int>(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.RemoveAt(state.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Permutations I */
|
||||
List<List<int>> PermutationsI(int[] nums) {
|
||||
List<List<int>> res = [];
|
||||
Backtrack([], nums, new bool[nums.Length], res);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = [1, 2, 3];
|
||||
|
||||
List<List<int>> res = PermutationsI(nums);
|
||||
|
||||
Console.WriteLine("Input array nums = " + string.Join(", ", nums));
|
||||
Console.WriteLine("All permutations res = ");
|
||||
foreach (List<int> permutation in res) {
|
||||
PrintUtil.PrintList(permutation);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* File: permutations_ii.cs
|
||||
* Created Time: 2023-04-24
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class permutations_ii {
|
||||
/* Backtracking algorithm: Permutations II */
|
||||
void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
|
||||
// When the state length equals the number of elements, record the solution
|
||||
if (state.Count == choices.Length) {
|
||||
res.Add(new List<int>(state));
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
HashSet<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.RemoveAt(state.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Permutations II */
|
||||
List<List<int>> PermutationsII(int[] nums) {
|
||||
List<List<int>> res = [];
|
||||
Backtrack([], nums, new bool[nums.Length], res);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = [1, 2, 2];
|
||||
|
||||
List<List<int>> res = PermutationsII(nums);
|
||||
|
||||
Console.WriteLine("Input array nums = " + string.Join(", ", nums));
|
||||
Console.WriteLine("All permutations res = ");
|
||||
foreach (List<int> permutation in res) {
|
||||
PrintUtil.PrintList(permutation);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* File: preorder_traversal_i_compact.cs
|
||||
* Created Time: 2023-04-17
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class preorder_traversal_i_compact {
|
||||
List<TreeNode> res = [];
|
||||
|
||||
/* Preorder traversal: Example 1 */
|
||||
void PreOrder(TreeNode? root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
if (root.val == 7) {
|
||||
// Record solution
|
||||
res.Add(root);
|
||||
}
|
||||
PreOrder(root.left);
|
||||
PreOrder(root.right);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\nInitialize binary tree");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
// Preorder traversal
|
||||
PreOrder(root);
|
||||
|
||||
Console.WriteLine("\nOutput all nodes with value 7");
|
||||
PrintUtil.PrintList(res.Select(p => p.val).ToList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* File: preorder_traversal_ii_compact.cs
|
||||
* Created Time: 2023-04-17
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class preorder_traversal_ii_compact {
|
||||
List<TreeNode> path = [];
|
||||
List<List<TreeNode>> res = [];
|
||||
|
||||
/* Preorder traversal: Example 2 */
|
||||
void PreOrder(TreeNode? root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
// Attempt
|
||||
path.Add(root);
|
||||
if (root.val == 7) {
|
||||
// Record solution
|
||||
res.Add(new List<TreeNode>(path));
|
||||
}
|
||||
PreOrder(root.left);
|
||||
PreOrder(root.right);
|
||||
// Backtrack
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\nInitialize binary tree");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
// Preorder traversal
|
||||
PreOrder(root);
|
||||
|
||||
Console.WriteLine("\nOutput all paths from root node to node 7");
|
||||
foreach (List<TreeNode> path in res) {
|
||||
PrintUtil.PrintList(path.Select(p => p.val).ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* File: preorder_traversal_iii_compact.cs
|
||||
* Created Time: 2023-04-17
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class preorder_traversal_iii_compact {
|
||||
List<TreeNode> path = [];
|
||||
List<List<TreeNode>> res = [];
|
||||
|
||||
/* Preorder traversal: Example 3 */
|
||||
void PreOrder(TreeNode? root) {
|
||||
// Pruning
|
||||
if (root == null || root.val == 3) {
|
||||
return;
|
||||
}
|
||||
// Attempt
|
||||
path.Add(root);
|
||||
if (root.val == 7) {
|
||||
// Record solution
|
||||
res.Add(new List<TreeNode>(path));
|
||||
}
|
||||
PreOrder(root.left);
|
||||
PreOrder(root.right);
|
||||
// Backtrack
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\nInitialize binary tree");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
// Preorder traversal
|
||||
PreOrder(root);
|
||||
|
||||
Console.WriteLine("\nOutput all paths from root node to node 7, paths do not include nodes with value 3");
|
||||
foreach (List<TreeNode> path in res) {
|
||||
PrintUtil.PrintList(path.Select(p => p.val).ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* File: preorder_traversal_iii_template.cs
|
||||
* Created Time: 2023-04-17
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class preorder_traversal_iii_template {
|
||||
/* Check if the current state is a solution */
|
||||
bool IsSolution(List<TreeNode> state) {
|
||||
return state.Count != 0 && state[^1].val == 7;
|
||||
}
|
||||
|
||||
/* Record solution */
|
||||
void RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
|
||||
res.Add(new List<TreeNode>(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.RemoveAt(state.Count - 1);
|
||||
}
|
||||
|
||||
/* 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
|
||||
foreach (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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\nInitialize binary tree");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
// Backtracking algorithm
|
||||
List<List<TreeNode>> res = [];
|
||||
List<TreeNode> choices = [root!];
|
||||
Backtrack([], choices, res);
|
||||
|
||||
Console.WriteLine("\nOutput all paths from root node to node 7, requiring paths do not include nodes with value 3");
|
||||
foreach (List<TreeNode> path in res) {
|
||||
PrintUtil.PrintList(path.Select(p => p.val).ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* File: subset_sum_i.cs
|
||||
* Created Time: 2023-06-25
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class subset_sum_i {
|
||||
/* Backtracking algorithm: Subset sum I */
|
||||
void Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {
|
||||
// When the subset sum equals target, record the solution
|
||||
if (target == 0) {
|
||||
res.Add(new List<int>(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.RemoveAt(state.Count - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Solve subset sum I */
|
||||
List<List<int>> SubsetSumI(int[] nums, int target) {
|
||||
List<int> state = []; // State (subset)
|
||||
Array.Sort(nums); // 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;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = [3, 4, 5];
|
||||
int target = 9;
|
||||
List<List<int>> res = SubsetSumI(nums, target);
|
||||
Console.WriteLine("Input array nums = " + string.Join(", ", nums) + ", target = " + target);
|
||||
Console.WriteLine("All subsets with sum equal to " + target + " are res = ");
|
||||
foreach (var subset in res) {
|
||||
PrintUtil.PrintList(subset);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* File: subset_sum_i_naive.cs
|
||||
* Created Time: 2023-06-25
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class subset_sum_i_naive {
|
||||
/* Backtracking algorithm: Subset sum I */
|
||||
void Backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {
|
||||
// When the subset sum equals target, record the solution
|
||||
if (total == target) {
|
||||
res.Add(new List<int>(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.RemoveAt(state.Count - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Solve subset sum I (including duplicate subsets) */
|
||||
List<List<int>> SubsetSumINaive(int[] nums, int target) {
|
||||
List<int> state = []; // State (subset)
|
||||
int total = 0; // Subset sum
|
||||
List<List<int>> res = []; // Result list (subset list)
|
||||
Backtrack(state, target, total, nums, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = [3, 4, 5];
|
||||
int target = 9;
|
||||
List<List<int>> res = SubsetSumINaive(nums, target);
|
||||
Console.WriteLine("Input array nums = " + string.Join(", ", nums) + ", target = " + target);
|
||||
Console.WriteLine("All subsets with sum equal to " + target + " are res = ");
|
||||
foreach (var subset in res) {
|
||||
PrintUtil.PrintList(subset);
|
||||
}
|
||||
Console.WriteLine("Please note that this method outputs results containing duplicate sets");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* File: subset_sum_ii.cs
|
||||
* Created Time: 2023-06-25
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class subset_sum_ii {
|
||||
/* Backtracking algorithm: Subset sum II */
|
||||
void Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {
|
||||
// When the subset sum equals target, record the solution
|
||||
if (target == 0) {
|
||||
res.Add(new List<int>(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.RemoveAt(state.Count - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Solve subset sum II */
|
||||
List<List<int>> SubsetSumII(int[] nums, int target) {
|
||||
List<int> state = []; // State (subset)
|
||||
Array.Sort(nums); // 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;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = [4, 4, 5];
|
||||
int target = 9;
|
||||
List<List<int>> res = SubsetSumII(nums, target);
|
||||
Console.WriteLine("Input array nums = " + string.Join(", ", nums) + ", target = " + target);
|
||||
Console.WriteLine("All subsets with sum equal to " + target + " are res = ");
|
||||
foreach (var subset in res) {
|
||||
PrintUtil.PrintList(subset);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user