hello-algo/codes/javascript/chapter_backtracking/subset_sum_i_naive.js
William Yuan 70784a1ec3
feat(chapter_backtracking): Add js and ts codes for chapter 13.3 (#667)
* 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
2023-08-03 14:44:49 +08:00

44 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 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('请注意,该方法输出的结果包含重复集合');