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

@@ -4,31 +4,31 @@
* Author: yuan0221 (yl1452491917@gmail.com)
*/
/* Backtracking algorithm: Subset sum II */
/* Backtracking: subset sum II */
function backtrack(state, target, choices, start, res) {
// When the subset sum equals target, record the solution
// Record a solution when the remaining target is zero, so the subset sum equals the original target
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
// Iterate over all choices
// Pruning 2: iterate from start to avoid generating duplicate subsets
// Pruning 3: start after the previously chosen index to avoid choosing the same element again
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
// Pruning 1: exit the loop if the next choice exceeds the remaining target
// The array is sorted, so later elements are at least as large and would also exceed the remaining 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
// Pruning 4: if this element equals its left neighbor at the same search depth, the branch is a duplicate; skip it
if (i > start && choices[i] === choices[i - 1]) {
continue;
}
// Attempt: make choice, update target, start
// Try: make a choice and update target and start
state.push(choices[i]);
// Proceed to the next round of selection
// Proceed to the next choice
backtrack(state, target - choices[i], choices, i + 1, res);
// Backtrack: undo choice, restore to previous state
// Backtrack: undo the choice and restore the previous state
state.pop();
}
}
@@ -37,15 +37,15 @@ function backtrack(state, target, choices, start, res) {
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)
const start = 0; // Starting index for iteration
const res = []; // Result list (list of subsets)
backtrack(state, target, nums, start, res);
return res;
}
/* Driver Code */
/* 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)}`);
console.log(`All subsets with sum ${target}: res = ${JSON.stringify(res)}`);