mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 13:16:29 +08:00
70784a1ec3
* feat(chapter_dynamic_programming): Add js and ts codes for chapter 14.1 * style(chapter_dynamic_programming): format code * refactor(chapter_dynamic_programming): remove main definition and add type * feat(chapter_backtracking): Add js and ts codes for chapter 13.3 * feat(chapter_divide_and_conquer): Add js and ts codes for chapter 12.2 * feat(chapter_divide_and_conquer): Add js and ts codes for chapter 12.3 * feat(chapter_divide_and_conquer): Add js and ts codes for chapter 12.4 * style(chapter_divide_and_conquer): fix typo * refactor: Use === instead of == in js and ts
44 lines
1.4 KiB
JavaScript
44 lines
1.4 KiB
JavaScript
/**
|
||
* File: subset_sum_i_naive.js
|
||
* Created Time: 2023-07-30
|
||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||
*/
|
||
|
||
/* 回溯算法:子集和 I */
|
||
function backtrack(state, target, total, choices, res) {
|
||
// 子集和等于 target 时,记录解
|
||
if (total === target) {
|
||
res.push([...state]);
|
||
return;
|
||
}
|
||
// 遍历所有选择
|
||
for (let i = 0; i < choices.length; i++) {
|
||
// 剪枝:若子集和超过 target ,则跳过该选择
|
||
if (total + choices[i] > target) {
|
||
continue;
|
||
}
|
||
// 尝试:做出选择,更新元素和 total
|
||
state.push(choices[i]);
|
||
// 进行下一轮选择
|
||
backtrack(state, target, total + choices[i], choices, res);
|
||
// 回退:撤销选择,恢复到之前的状态
|
||
state.pop();
|
||
}
|
||
}
|
||
|
||
/* 求解子集和 I(包含重复子集) */
|
||
function subsetSumINaive(nums, target) {
|
||
const state = []; // 状态(子集)
|
||
const total = 0; // 子集和
|
||
const res = []; // 结果列表(子集列表)
|
||
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(`输入数组 nums = ${JSON.stringify(nums)}, target = ${target}`);
|
||
console.log(`所有和等于 ${target} 的子集 res = ${JSON.stringify(res)}`);
|
||
console.log('请注意,该方法输出的结果包含重复集合');
|