mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-21 07:47:14 +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,67 @@
|
||||
/**
|
||||
* File: n_queens.swift
|
||||
* Created Time: 2023-05-14
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Backtracking algorithm: N queens */
|
||||
func backtrack(row: Int, n: Int, state: inout [[String]], res: inout [[[String]]], cols: inout [Bool], diags1: inout [Bool], diags2: inout [Bool]) {
|
||||
// When all rows are placed, record the solution
|
||||
if row == n {
|
||||
res.append(state)
|
||||
return
|
||||
}
|
||||
// Traverse all columns
|
||||
for col in 0 ..< n {
|
||||
// Calculate the main diagonal and anti-diagonal corresponding to this cell
|
||||
let diag1 = row - col + n - 1
|
||||
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"
|
||||
cols[col] = true
|
||||
diags1[diag1] = true
|
||||
diags2[diag2] = true
|
||||
// Place the next row
|
||||
backtrack(row: row + 1, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)
|
||||
// Backtrack: restore this cell to an empty cell
|
||||
state[row][col] = "#"
|
||||
cols[col] = false
|
||||
diags1[diag1] = false
|
||||
diags2[diag2] = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Solve N queens */
|
||||
func nQueens(n: Int) -> [[[String]]] {
|
||||
// Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell
|
||||
var state = Array(repeating: Array(repeating: "#", count: n), count: n)
|
||||
var cols = Array(repeating: false, count: n) // Record whether there is a queen in the column
|
||||
var diags1 = Array(repeating: false, count: 2 * n - 1) // Record whether there is a queen on the main diagonal
|
||||
var diags2 = Array(repeating: false, count: 2 * n - 1) // Record whether there is a queen on the anti-diagonal
|
||||
var res: [[[String]]] = []
|
||||
|
||||
backtrack(row: 0, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
@main
|
||||
enum NQueens {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let n = 4
|
||||
let res = nQueens(n: n)
|
||||
|
||||
print("Input board size is \(n)")
|
||||
print("Total queen placement solutions: \(res.count)")
|
||||
for state in res {
|
||||
print("--------------------")
|
||||
for row in state {
|
||||
print(row)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* File: permutations_i.swift
|
||||
* Created Time: 2023-04-30
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Backtracking algorithm: Permutations I */
|
||||
func backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {
|
||||
// When the state length equals the number of elements, record the solution
|
||||
if state.count == choices.count {
|
||||
res.append(state)
|
||||
return
|
||||
}
|
||||
// Traverse all choices
|
||||
for (i, choice) in choices.enumerated() {
|
||||
// Pruning: do not allow repeated selection of elements
|
||||
if !selected[i] {
|
||||
// Attempt: make choice, update state
|
||||
selected[i] = true
|
||||
state.append(choice)
|
||||
// Proceed to the next round of selection
|
||||
backtrack(state: &state, choices: choices, selected: &selected, res: &res)
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
selected[i] = false
|
||||
state.removeLast()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Permutations I */
|
||||
func permutationsI(nums: [Int]) -> [[Int]] {
|
||||
var state: [Int] = []
|
||||
var selected = Array(repeating: false, count: nums.count)
|
||||
var res: [[Int]] = []
|
||||
backtrack(state: &state, choices: nums, selected: &selected, res: &res)
|
||||
return res
|
||||
}
|
||||
|
||||
@main
|
||||
enum PermutationsI {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let nums = [1, 2, 3]
|
||||
|
||||
let res = permutationsI(nums: nums)
|
||||
|
||||
print("Input array nums = \(nums)")
|
||||
print("All permutations res = \(res)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* File: permutations_ii.swift
|
||||
* Created Time: 2023-04-30
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Backtracking algorithm: Permutations II */
|
||||
func backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {
|
||||
// When the state length equals the number of elements, record the solution
|
||||
if state.count == choices.count {
|
||||
res.append(state)
|
||||
return
|
||||
}
|
||||
// Traverse all choices
|
||||
var duplicated: Set<Int> = []
|
||||
for (i, choice) in choices.enumerated() {
|
||||
// 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.append(choice)
|
||||
// Proceed to the next round of selection
|
||||
backtrack(state: &state, choices: choices, selected: &selected, res: &res)
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
selected[i] = false
|
||||
state.removeLast()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Permutations II */
|
||||
func permutationsII(nums: [Int]) -> [[Int]] {
|
||||
var state: [Int] = []
|
||||
var selected = Array(repeating: false, count: nums.count)
|
||||
var res: [[Int]] = []
|
||||
backtrack(state: &state, choices: nums, selected: &selected, res: &res)
|
||||
return res
|
||||
}
|
||||
|
||||
@main
|
||||
enum PermutationsII {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let nums = [1, 2, 3]
|
||||
|
||||
let res = permutationsII(nums: nums)
|
||||
|
||||
print("Input array nums = \(nums)")
|
||||
print("All permutations res = \(res)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* File: preorder_traversal_i_compact.swift
|
||||
* Created Time: 2023-04-30
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
var res: [TreeNode] = []
|
||||
|
||||
/* Preorder traversal: Example 1 */
|
||||
func preOrder(root: TreeNode?) {
|
||||
guard let root = root else {
|
||||
return
|
||||
}
|
||||
if root.val == 7 {
|
||||
// Record solution
|
||||
res.append(root)
|
||||
}
|
||||
preOrder(root: root.left)
|
||||
preOrder(root: root.right)
|
||||
}
|
||||
|
||||
@main
|
||||
enum PreorderTraversalICompact {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let root = TreeNode.listToTree(arr: [1, 7, 3, 4, 5, 6, 7])
|
||||
print("\nInitialize binary tree")
|
||||
PrintUtil.printTree(root: root)
|
||||
|
||||
// Preorder traversal
|
||||
res = []
|
||||
preOrder(root: root)
|
||||
|
||||
print("\nOutput all nodes with value 7")
|
||||
var vals: [Int] = []
|
||||
for node in res {
|
||||
vals.append(node.val)
|
||||
}
|
||||
print(vals)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* File: preorder_traversal_ii_compact.swift
|
||||
* Created Time: 2023-04-30
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
var path: [TreeNode] = []
|
||||
var res: [[TreeNode]] = []
|
||||
|
||||
/* Preorder traversal: Example 2 */
|
||||
func preOrder(root: TreeNode?) {
|
||||
guard let root = root else {
|
||||
return
|
||||
}
|
||||
// Attempt
|
||||
path.append(root)
|
||||
if root.val == 7 {
|
||||
// Record solution
|
||||
res.append(path)
|
||||
}
|
||||
preOrder(root: root.left)
|
||||
preOrder(root: root.right)
|
||||
// Backtrack
|
||||
path.removeLast()
|
||||
}
|
||||
|
||||
@main
|
||||
enum PreorderTraversalIICompact {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let root = TreeNode.listToTree(arr: [1, 7, 3, 4, 5, 6, 7])
|
||||
print("\nInitialize binary tree")
|
||||
PrintUtil.printTree(root: root)
|
||||
|
||||
// Preorder traversal
|
||||
path = []
|
||||
res = []
|
||||
preOrder(root: root)
|
||||
|
||||
print("\nOutput all paths from root node to node 7")
|
||||
for path in res {
|
||||
var vals: [Int] = []
|
||||
for node in path {
|
||||
vals.append(node.val)
|
||||
}
|
||||
print(vals)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* File: preorder_traversal_iii_compact.swift
|
||||
* Created Time: 2023-04-30
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
var path: [TreeNode] = []
|
||||
var res: [[TreeNode]] = []
|
||||
|
||||
/* Preorder traversal: Example 3 */
|
||||
func preOrder(root: TreeNode?) {
|
||||
// Pruning
|
||||
guard let root = root, root.val != 3 else {
|
||||
return
|
||||
}
|
||||
// Attempt
|
||||
path.append(root)
|
||||
if root.val == 7 {
|
||||
// Record solution
|
||||
res.append(path)
|
||||
}
|
||||
preOrder(root: root.left)
|
||||
preOrder(root: root.right)
|
||||
// Backtrack
|
||||
path.removeLast()
|
||||
}
|
||||
|
||||
@main
|
||||
enum PreorderTraversalIIICompact {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let root = TreeNode.listToTree(arr: [1, 7, 3, 4, 5, 6, 7])
|
||||
print("\nInitialize binary tree")
|
||||
PrintUtil.printTree(root: root)
|
||||
|
||||
// Preorder traversal
|
||||
path = []
|
||||
res = []
|
||||
preOrder(root: root)
|
||||
|
||||
print("\nOutput all paths from root node to node 7, paths do not include nodes with value 3")
|
||||
for path in res {
|
||||
var vals: [Int] = []
|
||||
for node in path {
|
||||
vals.append(node.val)
|
||||
}
|
||||
print(vals)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* File: preorder_traversal_iii_template.swift
|
||||
* Created Time: 2023-04-30
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
/* Check if the current state is a solution */
|
||||
func isSolution(state: [TreeNode]) -> Bool {
|
||||
!state.isEmpty && state.last!.val == 7
|
||||
}
|
||||
|
||||
/* Record solution */
|
||||
func recordSolution(state: [TreeNode], res: inout [[TreeNode]]) {
|
||||
res.append(state)
|
||||
}
|
||||
|
||||
/* Check if the choice is valid under the current state */
|
||||
func isValid(state: [TreeNode], choice: TreeNode?) -> Bool {
|
||||
choice != nil && choice!.val != 3
|
||||
}
|
||||
|
||||
/* Update state */
|
||||
func makeChoice(state: inout [TreeNode], choice: TreeNode) {
|
||||
state.append(choice)
|
||||
}
|
||||
|
||||
/* Restore state */
|
||||
func undoChoice(state: inout [TreeNode], choice: TreeNode) {
|
||||
state.removeLast()
|
||||
}
|
||||
|
||||
/* Backtracking algorithm: Example 3 */
|
||||
func backtrack(state: inout [TreeNode], choices: [TreeNode], res: inout [[TreeNode]]) {
|
||||
// Check if it is a solution
|
||||
if isSolution(state: state) {
|
||||
recordSolution(state: state, res: &res)
|
||||
}
|
||||
// Traverse all choices
|
||||
for choice in choices {
|
||||
// Pruning: check if the choice is valid
|
||||
if isValid(state: state, choice: choice) {
|
||||
// Attempt: make choice, update state
|
||||
makeChoice(state: &state, choice: choice)
|
||||
// Proceed to the next round of selection
|
||||
backtrack(state: &state, choices: [choice.left, choice.right].compactMap { $0 }, res: &res)
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
undoChoice(state: &state, choice: choice)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
enum PreorderTraversalIIITemplate {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let root = TreeNode.listToTree(arr: [1, 7, 3, 4, 5, 6, 7])
|
||||
print("\nInitialize binary tree")
|
||||
PrintUtil.printTree(root: root)
|
||||
|
||||
// Backtracking algorithm
|
||||
var state: [TreeNode] = []
|
||||
var res: [[TreeNode]] = []
|
||||
backtrack(state: &state, choices: [root].compactMap { $0 }, res: &res)
|
||||
|
||||
print("\nOutput all paths from root node to node 7, paths do not include nodes with value 3")
|
||||
for path in res {
|
||||
var vals: [Int] = []
|
||||
for node in path {
|
||||
vals.append(node.val)
|
||||
}
|
||||
print(vals)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* File: subset_sum_i.swift
|
||||
* Created Time: 2023-07-02
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Backtracking algorithm: Subset sum I */
|
||||
func backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {
|
||||
// When the subset sum equals target, record the solution
|
||||
if target == 0 {
|
||||
res.append(state)
|
||||
return
|
||||
}
|
||||
// Traverse all choices
|
||||
// Pruning 2: start traversing from start to avoid generating duplicate subsets
|
||||
for i in choices.indices.dropFirst(start) {
|
||||
// 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(choices[i])
|
||||
// Proceed to the next round of selection
|
||||
backtrack(state: &state, target: target - choices[i], choices: choices, start: i, res: &res)
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
state.removeLast()
|
||||
}
|
||||
}
|
||||
|
||||
/* Solve subset sum I */
|
||||
func subsetSumI(nums: [Int], target: Int) -> [[Int]] {
|
||||
var state: [Int] = [] // State (subset)
|
||||
let nums = nums.sorted() // Sort nums
|
||||
let start = 0 // Start point for traversal
|
||||
var res: [[Int]] = [] // Result list (subset list)
|
||||
backtrack(state: &state, target: target, choices: nums, start: start, res: &res)
|
||||
return res
|
||||
}
|
||||
|
||||
@main
|
||||
enum SubsetSumI {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let nums = [3, 4, 5]
|
||||
let target = 9
|
||||
|
||||
let res = subsetSumI(nums: nums, target: target)
|
||||
|
||||
print("Input array nums = \(nums), target = \(target)")
|
||||
print("All subsets with sum equal to \(target) res = \(res)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* File: subset_sum_i_naive.swift
|
||||
* Created Time: 2023-07-02
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Backtracking algorithm: Subset sum I */
|
||||
func backtrack(state: inout [Int], target: Int, total: Int, choices: [Int], res: inout [[Int]]) {
|
||||
// When the subset sum equals target, record the solution
|
||||
if total == target {
|
||||
res.append(state)
|
||||
return
|
||||
}
|
||||
// Traverse all choices
|
||||
for i in choices.indices {
|
||||
// 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(choices[i])
|
||||
// Proceed to the next round of selection
|
||||
backtrack(state: &state, target: target, total: total + choices[i], choices: choices, res: &res)
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
state.removeLast()
|
||||
}
|
||||
}
|
||||
|
||||
/* Solve subset sum I (including duplicate subsets) */
|
||||
func subsetSumINaive(nums: [Int], target: Int) -> [[Int]] {
|
||||
var state: [Int] = [] // State (subset)
|
||||
let total = 0 // Subset sum
|
||||
var res: [[Int]] = [] // Result list (subset list)
|
||||
backtrack(state: &state, target: target, total: total, choices: nums, res: &res)
|
||||
return res
|
||||
}
|
||||
|
||||
@main
|
||||
enum SubsetSumINaive {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let nums = [3, 4, 5]
|
||||
let target = 9
|
||||
|
||||
let res = subsetSumINaive(nums: nums, target: 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,58 @@
|
||||
/**
|
||||
* File: subset_sum_ii.swift
|
||||
* Created Time: 2023-07-02
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Backtracking algorithm: Subset sum II */
|
||||
func backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {
|
||||
// When the subset sum equals target, record the solution
|
||||
if target == 0 {
|
||||
res.append(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 i in choices.indices.dropFirst(start) {
|
||||
// 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(choices[i])
|
||||
// Proceed to the next round of selection
|
||||
backtrack(state: &state, target: target - choices[i], choices: choices, start: i + 1, res: &res)
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
state.removeLast()
|
||||
}
|
||||
}
|
||||
|
||||
/* Solve subset sum II */
|
||||
func subsetSumII(nums: [Int], target: Int) -> [[Int]] {
|
||||
var state: [Int] = [] // State (subset)
|
||||
let nums = nums.sorted() // Sort nums
|
||||
let start = 0 // Start point for traversal
|
||||
var res: [[Int]] = [] // Result list (subset list)
|
||||
backtrack(state: &state, target: target, choices: nums, start: start, res: &res)
|
||||
return res
|
||||
}
|
||||
|
||||
@main
|
||||
enum SubsetSumII {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let nums = [4, 4, 5]
|
||||
let target = 9
|
||||
|
||||
let res = subsetSumII(nums: nums, target: target)
|
||||
|
||||
print("Input array nums = \(nums), target = \(target)")
|
||||
print("All subsets with sum equal to \(target) res = \(res)")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user