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,57 @@
// File: n_queens.go
// Created Time: 2023-05-09
// Author: Reanon (793584285@qq.com)
package chapter_backtracking
/* Backtracking algorithm: N queens */
func backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {
// When all rows are placed, record the solution
if row == n {
newState := make([][]string, len(*state))
for i, _ := range newState {
newState[i] = make([]string, len((*state)[0]))
copy(newState[i], (*state)[i])
}
*res = append(*res, newState)
return
}
// Traverse all columns
for col := 0; col < n; col++ {
// Calculate the main diagonal and anti-diagonal corresponding to this cell
diag1 := row - col + n - 1
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, 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] = "#"
(*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = false, false, false
}
}
}
/* Solve N queens */
func nQueens(n int) [][][]string {
// Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell
state := make([][]string, n)
for i := 0; i < n; i++ {
row := make([]string, n)
for i := 0; i < n; i++ {
row[i] = "#"
}
state[i] = row
}
// Record whether there is a queen in the column
cols := make([]bool, n)
diags1 := make([]bool, 2*n-1)
diags2 := make([]bool, 2*n-1)
res := make([][][]string, 0)
backtrack(0, n, &state, &res, &cols, &diags1, &diags2)
return res
}
@@ -0,0 +1,24 @@
// File: n_queens_test.go
// Created Time: 2023-05-14
// Author: Reanon (793584285@qq.com)
package chapter_backtracking
import (
"fmt"
"testing"
)
func TestNQueens(t *testing.T) {
n := 4
res := nQueens(n)
fmt.Println("Input board size is ", n)
fmt.Println("Total queen placement solutions: ", len(res), " solutions")
for _, state := range res {
fmt.Println("--------------------")
for _, row := range state {
fmt.Println(row)
}
}
}
@@ -0,0 +1,33 @@
// File: permutation_test.go
// Created Time: 2023-05-09
// Author: Reanon (793584285@qq.com)
package chapter_backtracking
import (
"fmt"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestPermutationI(t *testing.T) {
/* Permutations I */
nums := []int{1, 2, 3}
fmt.Printf("Input array nums = ")
PrintSlice(nums)
res := permutationsI(nums)
fmt.Printf("All permutations res = ")
fmt.Println(res)
}
func TestPermutationII(t *testing.T) {
nums := []int{1, 2, 2}
fmt.Printf("Input array nums = ")
PrintSlice(nums)
res := permutationsII(nums)
fmt.Printf("All permutations res = ")
fmt.Println(res)
}
@@ -0,0 +1,38 @@
// File: permutations_i.go
// Created Time: 2023-05-14
// Author: Reanon (793584285@qq.com)
package chapter_backtracking
/* Backtracking algorithm: Permutations I */
func backtrackI(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {
// When the state length equals the number of elements, record the solution
if len(*state) == len(*choices) {
newState := append([]int{}, *state...)
*res = append(*res, newState)
}
// Traverse all choices
for i := 0; i < len(*choices); i++ {
choice := (*choices)[i]
// Pruning: do not allow repeated selection of elements
if !(*selected)[i] {
// Attempt: make choice, update state
(*selected)[i] = true
*state = append(*state, choice)
// Proceed to the next round of selection
backtrackI(state, choices, selected, res)
// Backtrack: undo choice, restore to previous state
(*selected)[i] = false
*state = (*state)[:len(*state)-1]
}
}
}
/* Permutations I */
func permutationsI(nums []int) [][]int {
res := make([][]int, 0)
state := make([]int, 0)
selected := make([]bool, len(nums))
backtrackI(&state, &nums, &selected, &res)
return res
}
@@ -0,0 +1,41 @@
// File: permutations_ii.go
// Created Time: 2023-05-14
// Author: Reanon (793584285@qq.com)
package chapter_backtracking
/* Backtracking algorithm: Permutations II */
func backtrackII(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {
// When the state length equals the number of elements, record the solution
if len(*state) == len(*choices) {
newState := append([]int{}, *state...)
*res = append(*res, newState)
}
// Traverse all choices
duplicated := make(map[int]struct{}, 0)
for i := 0; i < len(*choices); i++ {
choice := (*choices)[i]
// Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements
if _, ok := duplicated[choice]; !ok && !(*selected)[i] {
// Attempt: make choice, update state
// Record the selected element value
duplicated[choice] = struct{}{}
(*selected)[i] = true
*state = append(*state, choice)
// Proceed to the next round of selection
backtrackII(state, choices, selected, res)
// Backtrack: undo choice, restore to previous state
(*selected)[i] = false
*state = (*state)[:len(*state)-1]
}
}
}
/* Permutations II */
func permutationsII(nums []int) [][]int {
res := make([][]int, 0)
state := make([]int, 0)
selected := make([]bool, len(nums))
backtrackII(&state, &nums, &selected, &res)
return res
}
@@ -0,0 +1,22 @@
// File: preorder_traversal_i_compact.go
// Created Time: 2023-05-09
// Author: Reanon (793584285@qq.com)
package chapter_backtracking
import (
. "github.com/krahets/hello-algo/pkg"
)
/* Preorder traversal: Example 1 */
func preOrderI(root *TreeNode, res *[]*TreeNode) {
if root == nil {
return
}
if (root.Val).(int) == 7 {
// Record solution
*res = append(*res, root)
}
preOrderI(root.Left, res)
preOrderI(root.Right, res)
}
@@ -0,0 +1,26 @@
// File: preorder_traversal_ii_compact.go
// Created Time: 2023-05-09
// Author: Reanon (793584285@qq.com)
package chapter_backtracking
import (
. "github.com/krahets/hello-algo/pkg"
)
/* Preorder traversal: Example 2 */
func preOrderII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {
if root == nil {
return
}
// Attempt
*path = append(*path, root)
if root.Val.(int) == 7 {
// Record solution
*res = append(*res, append([]*TreeNode{}, *path...))
}
preOrderII(root.Left, res, path)
preOrderII(root.Right, res, path)
// Backtrack
*path = (*path)[:len(*path)-1]
}
@@ -0,0 +1,27 @@
// File: preorder_traversal_iii_compact.go
// Created Time: 2023-05-09
// Author: Reanon (793584285@qq.com)
package chapter_backtracking
import (
. "github.com/krahets/hello-algo/pkg"
)
/* Preorder traversal: Example 3 */
func preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {
// Pruning
if root == nil || root.Val == 3 {
return
}
// Attempt
*path = append(*path, root)
if root.Val.(int) == 7 {
// Record solution
*res = append(*res, append([]*TreeNode{}, *path...))
}
preOrderIII(root.Left, res, path)
preOrderIII(root.Right, res, path)
// Backtrack
*path = (*path)[:len(*path)-1]
}
@@ -0,0 +1,57 @@
// File: preorder_traversal_iii_template.go
// Created Time: 2023-05-09
// Author: Reanon (793584285@qq.com)
package chapter_backtracking
import (
. "github.com/krahets/hello-algo/pkg"
)
/* Check if the current state is a solution */
func isSolution(state *[]*TreeNode) bool {
return len(*state) != 0 && (*state)[len(*state)-1].Val == 7
}
/* Record solution */
func recordSolution(state *[]*TreeNode, res *[][]*TreeNode) {
*res = append(*res, append([]*TreeNode{}, *state...))
}
/* Check if the choice is valid under the current state */
func isValid(state *[]*TreeNode, choice *TreeNode) bool {
return choice != nil && choice.Val != 3
}
/* Update state */
func makeChoice(state *[]*TreeNode, choice *TreeNode) {
*state = append(*state, choice)
}
/* Restore state */
func undoChoice(state *[]*TreeNode, choice *TreeNode) {
*state = (*state)[:len(*state)-1]
}
/* Backtracking algorithm: Example 3 */
func backtrackIII(state *[]*TreeNode, choices *[]*TreeNode, res *[][]*TreeNode) {
// Check if it is a solution
if isSolution(state) {
// Record solution
recordSolution(state, res)
}
// Traverse all choices
for _, choice := range *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
temp := make([]*TreeNode, 0)
temp = append(temp, choice.Left, choice.Right)
backtrackIII(state, &temp, res)
// Backtrack: undo choice, restore to previous state
undoChoice(state, choice)
}
}
}
@@ -0,0 +1,91 @@
// File: preorder_traversal_i_compact_test.go
// Created Time: 2023-05-09
// Author: Reanon (793584285@qq.com)
package chapter_backtracking
import (
"fmt"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestPreorderTraversalICompact(t *testing.T) {
/* Initialize binary tree */
root := SliceToTree([]any{1, 7, 3, 4, 5, 6, 7})
fmt.Println("\nInitialize binary tree")
PrintTree(root)
// Preorder traversal
res := make([]*TreeNode, 0)
preOrderI(root, &res)
fmt.Println("\nOutput all nodes with value 7")
for _, node := range res {
fmt.Printf("%v ", node.Val)
}
fmt.Println()
}
func TestPreorderTraversalIICompact(t *testing.T) {
/* Initialize binary tree */
root := SliceToTree([]any{1, 7, 3, 4, 5, 6, 7})
fmt.Println("\nInitialize binary tree")
PrintTree(root)
// Preorder traversal
path := make([]*TreeNode, 0)
res := make([][]*TreeNode, 0)
preOrderII(root, &res, &path)
fmt.Println("\nOutput all paths from root node to node 7")
for _, path := range res {
for _, node := range path {
fmt.Printf("%v ", node.Val)
}
fmt.Println()
}
}
func TestPreorderTraversalIIICompact(t *testing.T) {
/* Initialize binary tree */
root := SliceToTree([]any{1, 7, 3, 4, 5, 6, 7})
fmt.Println("\nInitialize binary tree")
PrintTree(root)
// Preorder traversal
path := make([]*TreeNode, 0)
res := make([][]*TreeNode, 0)
preOrderIII(root, &res, &path)
fmt.Println("\nOutput all paths from root node to node 7, paths do not include nodes with value 3")
for _, path := range res {
for _, node := range path {
fmt.Printf("%v ", node.Val)
}
fmt.Println()
}
}
func TestPreorderTraversalIIITemplate(t *testing.T) {
/* Initialize binary tree */
root := SliceToTree([]any{1, 7, 3, 4, 5, 6, 7})
fmt.Println("\nInitialize binary tree")
PrintTree(root)
// Backtracking algorithm
res := make([][]*TreeNode, 0)
state := make([]*TreeNode, 0)
choices := make([]*TreeNode, 0)
choices = append(choices, root)
backtrackIII(&state, &choices, &res)
fmt.Println("\nOutput all paths from root node to node 7, paths do not include nodes with value 3")
for _, path := range res {
for _, node := range path {
fmt.Printf("%v ", node.Val)
}
fmt.Println()
}
}
@@ -0,0 +1,42 @@
// File: subset_sum_i.go
// Created Time: 2023-06-24
// Author: Reanon (793584285@qq.com)
package chapter_backtracking
import "sort"
/* Backtracking algorithm: Subset sum I */
func backtrackSubsetSumI(start, target int, state, choices *[]int, res *[][]int) {
// When the subset sum equals target, record the solution
if target == 0 {
newState := append([]int{}, *state...)
*res = append(*res, newState)
return
}
// Traverse all choices
// Pruning 2: start traversing from start to avoid generating duplicate subsets
for i := start; i < len(*choices); 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 = append(*state, (*choices)[i])
// Proceed to the next round of selection
backtrackSubsetSumI(i, target-(*choices)[i], state, choices, res)
// Backtrack: undo choice, restore to previous state
*state = (*state)[:len(*state)-1]
}
}
/* Solve subset sum I */
func subsetSumI(nums []int, target int) [][]int {
state := make([]int, 0) // State (subset)
sort.Ints(nums) // Sort nums
start := 0 // Start point for traversal
res := make([][]int, 0) // Result list (subset list)
backtrackSubsetSumI(start, target, &state, &nums, &res)
return res
}
@@ -0,0 +1,37 @@
// File: subset_sum_i_naive.go
// Created Time: 2023-06-24
// Author: Reanon (793584285@qq.com)
package chapter_backtracking
/* Backtracking algorithm: Subset sum I */
func backtrackSubsetSumINaive(total, target int, state, choices *[]int, res *[][]int) {
// When the subset sum equals target, record the solution
if target == total {
newState := append([]int{}, *state...)
*res = append(*res, newState)
return
}
// Traverse all choices
for i := 0; i < len(*choices); 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 = append(*state, (*choices)[i])
// Proceed to the next round of selection
backtrackSubsetSumINaive(total+(*choices)[i], target, state, choices, res)
// Backtrack: undo choice, restore to previous state
*state = (*state)[:len(*state)-1]
}
}
/* Solve subset sum I (including duplicate subsets) */
func subsetSumINaive(nums []int, target int) [][]int {
state := make([]int, 0) // State (subset)
total := 0 // Subset sum
res := make([][]int, 0) // Result list (subset list)
backtrackSubsetSumINaive(total, target, &state, &nums, &res)
return res
}
@@ -0,0 +1,47 @@
// File: subset_sum_ii.go
// Created Time: 2023-06-24
// Author: Reanon (793584285@qq.com)
package chapter_backtracking
import "sort"
/* Backtracking algorithm: Subset sum II */
func backtrackSubsetSumII(start, target int, state, choices *[]int, res *[][]int) {
// When the subset sum equals target, record the solution
if target == 0 {
newState := append([]int{}, *state...)
*res = append(*res, newState)
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 := start; i < len(*choices); 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 = append(*state, (*choices)[i])
// Proceed to the next round of selection
backtrackSubsetSumII(i+1, target-(*choices)[i], state, choices, res)
// Backtrack: undo choice, restore to previous state
*state = (*state)[:len(*state)-1]
}
}
/* Solve subset sum II */
func subsetSumII(nums []int, target int) [][]int {
state := make([]int, 0) // State (subset)
sort.Ints(nums) // Sort nums
start := 0 // Start point for traversal
res := make([][]int, 0) // Result list (subset list)
backtrackSubsetSumII(start, target, &state, &nums, &res)
return res
}
@@ -0,0 +1,56 @@
// File: subset_sum_test.go
// Created Time: 2023-06-24
// Author: Reanon (793584285@qq.com)
package chapter_backtracking
import (
"fmt"
"strconv"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestSubsetSumINaive(t *testing.T) {
nums := []int{3, 4, 5}
target := 9
res := subsetSumINaive(nums, target)
fmt.Printf("target = " + strconv.Itoa(target) + ", input array nums = ")
PrintSlice(nums)
fmt.Println("All subsets with sum equal to " + strconv.Itoa(target) + " are res = ")
for i := range res {
PrintSlice(res[i])
}
fmt.Println("Please note that this method outputs results containing duplicate sets")
}
func TestSubsetSumI(t *testing.T) {
nums := []int{3, 4, 5}
target := 9
res := subsetSumI(nums, target)
fmt.Printf("target = " + strconv.Itoa(target) + ", input array nums = ")
PrintSlice(nums)
fmt.Println("All subsets with sum equal to " + strconv.Itoa(target) + " are res = ")
for i := range res {
PrintSlice(res[i])
}
}
func TestSubsetSumII(t *testing.T) {
nums := []int{4, 4, 5}
target := 9
res := subsetSumII(nums, target)
fmt.Printf("target = " + strconv.Itoa(target) + ", input array nums = ")
PrintSlice(nums)
fmt.Println("All subsets with sum equal to " + strconv.Itoa(target) + " are res = ")
for i := range res {
PrintSlice(res[i])
}
}