Retranslate the complete English edition from Simplified Chinese (#2010)

* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
This commit is contained in:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -6,20 +6,20 @@
/* Binary search: problem f(i, j) */
function dfs(nums, target, i, j) {
// If the interval is empty, it means there is no target element, return -1
// If the interval is empty, the target is absent; return -1
if (i > j) {
return -1;
}
// Calculate the midpoint index m
const m = i + ((j - i) >> 1);
if (nums[m] < target) {
// Recursion subproblem f(m+1, j)
// Recurse on subproblem f(m+1, j)
return dfs(nums, target, m + 1, j);
} else if (nums[m] > target) {
// Recursion subproblem f(i, m-1)
// Recurse on subproblem f(i, m-1)
return dfs(nums, target, i, m - 1);
} else {
// Found the target element, return its index
// The target element was found; return its index
return m;
}
}
@@ -27,13 +27,13 @@ function dfs(nums, target, i, j) {
/* Binary search */
function binarySearch(nums, target) {
const n = nums.length;
// Solve the problem f(0, n-1)
// Solve problem f(0, n-1)
return dfs(nums, target, 0, n - 1);
}
/* Driver Code */
/* Driver code */
const target = 6;
const nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
// Binary search (closed interval on both sides)
// Binary search (closed interval)
const index = binarySearch(nums, target);
console.log(`Index of target element 6 is ${index}`);
console.log(`Index of target element 6 = ${index}`);

View File

@@ -7,13 +7,13 @@
const { printTree } = require('../modules/PrintUtil');
const { TreeNode } = require('../modules/TreeNode');
/* Build binary tree: divide and conquer */
/* Build a binary tree: divide and conquer */
function dfs(preorder, inorderMap, i, l, r) {
// Terminate when the subtree interval is empty
// Stop when the subtree interval is empty
if (r - l < 0) return null;
// Initialize the root node
const root = new TreeNode(preorder[i]);
// Query m to divide the left and right subtrees
// Look up m to separate the left and right subtrees
const m = inorderMap.get(preorder[i]);
// Subproblem: build the left subtree
root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
@@ -23,9 +23,9 @@ function dfs(preorder, inorderMap, i, l, r) {
return root;
}
/* Build binary tree */
/* Build a binary tree */
function buildTree(preorder, inorder) {
// Initialize hash map, storing the mapping from inorder elements to indices
// Initialize a hash table mapping each element in inorder to its index
let inorderMap = new Map();
for (let i = 0; i < inorder.length; i++) {
inorderMap.set(inorder[i], i);
@@ -34,7 +34,7 @@ function buildTree(preorder, inorder) {
return root;
}
/* Driver Code */
/* Driver code */
const preorder = [3, 9, 2, 1, 7];
const inorder = [9, 3, 1, 2, 7];
console.log('Preorder traversal = ' + JSON.stringify(preorder));

View File

@@ -4,7 +4,7 @@
* Author: Hello Algo Team
*/
/* Exponentiation by squaring */
/* Fast exponentiation */
function fastPow(x, n) {
if (n === 0) {
return 1;
@@ -17,5 +17,5 @@ function fastPow(x, n) {
}
if (fastPow(7, 0) !== 1 || fastPow(3, 5) !== 243 || fastPow(2, 6) !== 64) {
throw new Error('fast power check failed');
throw new Error('Fast power check failed');
}

View File

@@ -4,9 +4,9 @@
* Author: yuan0221 (yl1452491917@gmail.com)
*/
/* Move a disk */
/* Move one disk */
function move(src, tar) {
// Take out a disk from the top of src
// Take a disk from the top of src
const pan = src.pop();
// Place the disk on top of tar
tar.push(pan);
@@ -14,14 +14,14 @@ function move(src, tar) {
/* Solve the Tower of Hanoi problem f(i) */
function dfs(i, src, buf, tar) {
// If there is only one disk left in src, move it directly to tar
// If only one disk remains to be moved from src, move it directly to tar
if (i === 1) {
move(src, tar);
return;
}
// Subproblem f(i-1): move the top i-1 disks from src to buf using tar
dfs(i - 1, src, tar, buf);
// Subproblem f(1): move the remaining disk from src to tar
// Subproblem f(1): move the remaining disk of this subproblem from src to tar
move(src, tar);
// Subproblem f(i-1): move the top i-1 disks from buf to tar using src
dfs(i - 1, buf, src, tar);
@@ -34,19 +34,19 @@ function solveHanota(A, B, C) {
dfs(n, A, B, C);
}
/* Driver Code */
// The tail of the list is the top of the rod
/* Driver code */
// The end of each list represents the top of the peg
const A = [5, 4, 3, 2, 1];
const B = [];
const C = [];
console.log('In initial state:');
console.log('Initial state:');
console.log(`A = ${JSON.stringify(A)}`);
console.log(`B = ${JSON.stringify(B)}`);
console.log(`C = ${JSON.stringify(C)}`);
solveHanota(A, B, C);
console.log('After disk movement is complete:');
console.log('After moving the disks:');
console.log(`A = ${JSON.stringify(A)}`);
console.log(`B = ${JSON.stringify(B)}`);
console.log(`C = ${JSON.stringify(C)}`);