mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08: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:
55
en/codes/javascript/chapter_backtracking/n_queens.js
Normal file
55
en/codes/javascript/chapter_backtracking/n_queens.js
Normal file
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* File: n_queens.js
|
||||
* Created Time: 2023-05-13
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/* Backtracking algorithm: N queens */
|
||||
function backtrack(row, n, state, res, cols, diags1, diags2) {
|
||||
// When all rows are placed, record the solution
|
||||
if (row === n) {
|
||||
res.push(state.map((row) => row.slice()));
|
||||
return;
|
||||
}
|
||||
// Traverse all columns
|
||||
for (let col = 0; col < n; col++) {
|
||||
// Calculate the main diagonal and anti-diagonal corresponding to this cell
|
||||
const diag1 = row - col + n - 1;
|
||||
const 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 */
|
||||
function nQueens(n) {
|
||||
// Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell
|
||||
const state = Array.from({ length: n }, () => Array(n).fill('#'));
|
||||
const cols = Array(n).fill(false); // Record whether there is a queen in the column
|
||||
const diags1 = Array(2 * n - 1).fill(false); // Record whether there is a queen on the main diagonal
|
||||
const diags2 = Array(2 * n - 1).fill(false); // Record whether there is a queen on the anti-diagonal
|
||||
const res = [];
|
||||
|
||||
backtrack(0, n, state, res, cols, diags1, diags2);
|
||||
return res;
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
const n = 4;
|
||||
const res = nQueens(n);
|
||||
|
||||
console.log(`Input board size is ${n}`);
|
||||
console.log(`Total queen placement solutions: ${res.length}`);
|
||||
res.forEach((state) => {
|
||||
console.log('--------------------');
|
||||
state.forEach((row) => console.log(row));
|
||||
});
|
||||
42
en/codes/javascript/chapter_backtracking/permutations_i.js
Normal file
42
en/codes/javascript/chapter_backtracking/permutations_i.js
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* File: permutations_i.js
|
||||
* Created Time: 2023-05-13
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/* Backtracking algorithm: Permutations I */
|
||||
function backtrack(state, choices, selected, res) {
|
||||
// When the state length equals the number of elements, record the solution
|
||||
if (state.length === choices.length) {
|
||||
res.push([...state]);
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
choices.forEach((choice, 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, choices, selected, res);
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
selected[i] = false;
|
||||
state.pop();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* Permutations I */
|
||||
function permutationsI(nums) {
|
||||
const res = [];
|
||||
backtrack([], nums, Array(nums.length).fill(false), res);
|
||||
return res;
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
const nums = [1, 2, 3];
|
||||
const res = permutationsI(nums);
|
||||
|
||||
console.log(`Input array nums = ${JSON.stringify(nums)}`);
|
||||
console.log(`All permutations res = ${JSON.stringify(res)}`);
|
||||
44
en/codes/javascript/chapter_backtracking/permutations_ii.js
Normal file
44
en/codes/javascript/chapter_backtracking/permutations_ii.js
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* File: permutations_ii.js
|
||||
* Created Time: 2023-05-13
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/* Backtracking algorithm: Permutations II */
|
||||
function backtrack(state, choices, selected, res) {
|
||||
// When the state length equals the number of elements, record the solution
|
||||
if (state.length === choices.length) {
|
||||
res.push([...state]);
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
const duplicated = new Set();
|
||||
choices.forEach((choice, i) => {
|
||||
// Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements
|
||||
if (!selected[i] && !duplicated.has(choice)) {
|
||||
// Attempt: make choice, update state
|
||||
duplicated.add(choice); // Record the selected element value
|
||||
selected[i] = true;
|
||||
state.push(choice);
|
||||
// Proceed to the next round of selection
|
||||
backtrack(state, choices, selected, res);
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
selected[i] = false;
|
||||
state.pop();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* Permutations II */
|
||||
function permutationsII(nums) {
|
||||
const res = [];
|
||||
backtrack([], nums, Array(nums.length).fill(false), res);
|
||||
return res;
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
const nums = [1, 2, 2];
|
||||
const res = permutationsII(nums);
|
||||
|
||||
console.log(`Input array nums = ${JSON.stringify(nums)}`);
|
||||
console.log(`All permutations res = ${JSON.stringify(res)}`);
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* File: preorder_traversal_i_compact.js
|
||||
* Created Time: 2023-05-09
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
const { arrToTree } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* Preorder traversal: Example 1 */
|
||||
function preOrder(root, res) {
|
||||
if (root === null) {
|
||||
return;
|
||||
}
|
||||
if (root.val === 7) {
|
||||
// Record solution
|
||||
res.push(root);
|
||||
}
|
||||
preOrder(root.left, res);
|
||||
preOrder(root.right, res);
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
console.log('\nInitialize binary tree');
|
||||
printTree(root);
|
||||
|
||||
// Preorder traversal
|
||||
const res = [];
|
||||
preOrder(root, res);
|
||||
|
||||
console.log('\nOutput all nodes with value 7');
|
||||
console.log(res.map((node) => node.val));
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* File: preorder_traversal_ii_compact.js
|
||||
* Created Time: 2023-05-09
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
const { arrToTree } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* Preorder traversal: Example 2 */
|
||||
function preOrder(root, path, res) {
|
||||
if (root === null) {
|
||||
return;
|
||||
}
|
||||
// Attempt
|
||||
path.push(root);
|
||||
if (root.val === 7) {
|
||||
// Record solution
|
||||
res.push([...path]);
|
||||
}
|
||||
preOrder(root.left, path, res);
|
||||
preOrder(root.right, path, res);
|
||||
// Backtrack
|
||||
path.pop();
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
console.log('\nInitialize binary tree');
|
||||
printTree(root);
|
||||
|
||||
// Preorder traversal
|
||||
const path = [];
|
||||
const res = [];
|
||||
preOrder(root, path, res);
|
||||
|
||||
console.log('\nOutput all paths from root node to node 7');
|
||||
res.forEach((path) => {
|
||||
console.log(path.map((node) => node.val));
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* File: preorder_traversal_iii_compact.js
|
||||
* Created Time: 2023-05-09
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
const { arrToTree } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* Preorder traversal: Example 3 */
|
||||
function preOrder(root, path, res) {
|
||||
// Pruning
|
||||
if (root === null || root.val === 3) {
|
||||
return;
|
||||
}
|
||||
// Attempt
|
||||
path.push(root);
|
||||
if (root.val === 7) {
|
||||
// Record solution
|
||||
res.push([...path]);
|
||||
}
|
||||
preOrder(root.left, path, res);
|
||||
preOrder(root.right, path, res);
|
||||
// Backtrack
|
||||
path.pop();
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
console.log('\nInitialize binary tree');
|
||||
printTree(root);
|
||||
|
||||
// Preorder traversal
|
||||
const path = [];
|
||||
const res = [];
|
||||
preOrder(root, path, res);
|
||||
|
||||
console.log('\nOutput all paths from root node to node 7, paths do not include nodes with value 3');
|
||||
res.forEach((path) => {
|
||||
console.log(path.map((node) => node.val));
|
||||
});
|
||||
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* File: preorder_traversal_iii_template.js
|
||||
* Created Time: 2023-05-09
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
const { arrToTree } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* Check if the current state is a solution */
|
||||
function isSolution(state) {
|
||||
return state && state[state.length - 1]?.val === 7;
|
||||
}
|
||||
|
||||
/* Record solution */
|
||||
function recordSolution(state, res) {
|
||||
res.push([...state]);
|
||||
}
|
||||
|
||||
/* Check if the choice is valid under the current state */
|
||||
function isValid(state, choice) {
|
||||
return choice !== null && choice.val !== 3;
|
||||
}
|
||||
|
||||
/* Update state */
|
||||
function makeChoice(state, choice) {
|
||||
state.push(choice);
|
||||
}
|
||||
|
||||
/* Restore state */
|
||||
function undoChoice(state) {
|
||||
state.pop();
|
||||
}
|
||||
|
||||
/* Backtracking algorithm: Example 3 */
|
||||
function backtrack(state, choices, res) {
|
||||
// Check if it is a solution
|
||||
if (isSolution(state)) {
|
||||
// Record solution
|
||||
recordSolution(state, res);
|
||||
}
|
||||
// Traverse all choices
|
||||
for (const choice of 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
console.log('\nInitialize binary tree');
|
||||
printTree(root);
|
||||
|
||||
// Backtracking algorithm
|
||||
const res = [];
|
||||
backtrack([], [root], res);
|
||||
|
||||
console.log('\nOutput all paths from root node to node 7, requiring paths do not include nodes with value 3');
|
||||
res.forEach((path) => {
|
||||
console.log(path.map((node) => node.val));
|
||||
});
|
||||
46
en/codes/javascript/chapter_backtracking/subset_sum_i.js
Normal file
46
en/codes/javascript/chapter_backtracking/subset_sum_i.js
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* File: subset_sum_i.js
|
||||
* Created Time: 2023-07-30
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* Backtracking algorithm: Subset sum I */
|
||||
function backtrack(state, target, choices, start, res) {
|
||||
// When the subset sum equals target, record the solution
|
||||
if (target === 0) {
|
||||
res.push([...state]);
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
// Pruning 2: start traversing from start to avoid generating duplicate subsets
|
||||
for (let 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.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 */
|
||||
function subsetSumI(nums, target) {
|
||||
const state = []; // State (subset)
|
||||
nums.sort((a, b) => a - b); // Sort nums
|
||||
const start = 0; // Start point for traversal
|
||||
const res = []; // Result list (subset list)
|
||||
backtrack(state, target, nums, start, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const nums = [3, 4, 5];
|
||||
const target = 9;
|
||||
const res = subsetSumI(nums, target);
|
||||
console.log(`Input array nums = ${JSON.stringify(nums)}, target = ${target}`);
|
||||
console.log(`All subsets with sum equal to ${target} res = ${JSON.stringify(res)}`);
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* File: subset_sum_i_naive.js
|
||||
* Created Time: 2023-07-30
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* Backtracking algorithm: Subset sum I */
|
||||
function backtrack(state, target, total, choices, res) {
|
||||
// When the subset sum equals target, record the solution
|
||||
if (total === target) {
|
||||
res.push([...state]);
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
for (let 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.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) */
|
||||
function subsetSumINaive(nums, target) {
|
||||
const state = []; // State (subset)
|
||||
const total = 0; // Subset sum
|
||||
const res = []; // Result list (subset list)
|
||||
backtrack(state, target, total, nums, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const nums = [3, 4, 5];
|
||||
const target = 9;
|
||||
const res = subsetSumINaive(nums, target);
|
||||
console.log(`Input array nums = ${JSON.stringify(nums)}, target = ${target}`);
|
||||
console.log(`All subsets with sum equal to ${target} res = ${JSON.stringify(res)}`);
|
||||
console.log('Please note that this method outputs results containing duplicate sets');
|
||||
51
en/codes/javascript/chapter_backtracking/subset_sum_ii.js
Normal file
51
en/codes/javascript/chapter_backtracking/subset_sum_ii.js
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* File: subset_sum_ii.js
|
||||
* Created Time: 2023-07-30
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* Backtracking algorithm: Subset sum II */
|
||||
function backtrack(state, target, choices, start, res) {
|
||||
// When the subset sum equals target, record the solution
|
||||
if (target === 0) {
|
||||
res.push([...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 (let 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.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 */
|
||||
function subsetSumII(nums, target) {
|
||||
const state = []; // State (subset)
|
||||
nums.sort((a, b) => a - b); // Sort nums
|
||||
const start = 0; // Start point for traversal
|
||||
const res = []; // Result list (subset list)
|
||||
backtrack(state, target, nums, start, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const nums = [4, 4, 5];
|
||||
const target = 9;
|
||||
const res = subsetSumII(nums, target);
|
||||
console.log(`Input array nums = ${JSON.stringify(nums)}, target = ${target}`);
|
||||
console.log(`All subsets with sum equal to ${target} res = ${JSON.stringify(res)}`);
|
||||
Reference in New Issue
Block a user