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 tspull/664/head^2
parent
c7c33f19ac
commit
70784a1ec3
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* File: subset_sum_i.js
|
||||
* Created Time: 2023-07-30
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* 回溯算法:子集和 I */
|
||||
function backtrack(state, target, choices, start, res) {
|
||||
// 子集和等于 target 时,记录解
|
||||
if (target === 0) {
|
||||
res.push([...state]);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
// 剪枝二:从 start 开始遍历,避免生成重复子集
|
||||
for (let i = start; i < choices.length; i++) {
|
||||
// 剪枝一:若子集和超过 target ,则直接结束循环
|
||||
// 这是因为数组已排序,后边元素更大,子集和一定超过 target
|
||||
if (target - choices[i] < 0) {
|
||||
break;
|
||||
}
|
||||
// 尝试:做出选择,更新 target, start
|
||||
state.push(choices[i]);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, target - choices[i], choices, i, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
state.pop();
|
||||
}
|
||||
}
|
||||
|
||||
/* 求解子集和 I */
|
||||
function subsetSumI(nums, target) {
|
||||
const state = []; // 状态(子集)
|
||||
nums.sort(); // 对 nums 进行排序
|
||||
const start = 0; // 遍历起始点
|
||||
const res = []; // 结果列表(子集列表)
|
||||
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(`输入数组 nums = ${JSON.stringify(nums)}, target = ${target}`);
|
||||
console.log(`所有和等于 ${target} 的子集 res = ${JSON.stringify(res)}`);
|
@ -0,0 +1,51 @@
|
||||
/**
|
||||
* File: subset_sum_ii.js
|
||||
* Created Time: 2023-07-30
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* 回溯算法:子集和 II */
|
||||
function backtrack(state, target, choices, start, res) {
|
||||
// 子集和等于 target 时,记录解
|
||||
if (target === 0) {
|
||||
res.push([...state]);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
// 剪枝二:从 start 开始遍历,避免生成重复子集
|
||||
// 剪枝三:从 start 开始遍历,避免重复选择同一元素
|
||||
for (let i = start; i < choices.length; i++) {
|
||||
// 剪枝一:若子集和超过 target ,则直接结束循环
|
||||
// 这是因为数组已排序,后边元素更大,子集和一定超过 target
|
||||
if (target - choices[i] < 0) {
|
||||
break;
|
||||
}
|
||||
// 剪枝四:如果该元素与左边元素相等,说明该搜索分支重复,直接跳过
|
||||
if (i > start && choices[i] === choices[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
// 尝试:做出选择,更新 target, start
|
||||
state.push(choices[i]);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, target - choices[i], choices, i + 1, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
state.pop();
|
||||
}
|
||||
}
|
||||
|
||||
/* 求解子集和 II */
|
||||
function subsetSumII(nums, target) {
|
||||
const state = []; // 状态(子集)
|
||||
nums.sort(); // 对 nums 进行排序
|
||||
const start = 0; // 遍历起始点
|
||||
const res = []; // 结果列表(子集列表)
|
||||
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(`输入数组 nums = ${JSON.stringify(nums)}, target = ${target}`);
|
||||
console.log(`所有和等于 ${target} 的子集 res = ${JSON.stringify(res)}`);
|
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* File: binary_search_recur.js
|
||||
* Created Time: 2023-07-30
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* 二分查找:问题 f(i, j) */
|
||||
function dfs(nums, target, i, j) {
|
||||
// 若区间为空,代表无目标元素,则返回 -1
|
||||
if (i > j) {
|
||||
return -1;
|
||||
}
|
||||
// 计算中点索引 m
|
||||
const m = i + ((j - i) >> 1);
|
||||
if (nums[m] < target) {
|
||||
// 递归子问题 f(m+1, j)
|
||||
return dfs(nums, target, m + 1, j);
|
||||
} else if (nums[m] > target) {
|
||||
// 递归子问题 f(i, m-1)
|
||||
return dfs(nums, target, i, m - 1);
|
||||
} else {
|
||||
// 找到目标元素,返回其索引
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/* 二分查找 */
|
||||
function binarySearch(nums, target) {
|
||||
const n = nums.length;
|
||||
// 求解问题 f(0, n-1)
|
||||
return dfs(nums, target, 0, n - 1);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const target = 6;
|
||||
const nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
|
||||
// 二分查找(双闭区间)
|
||||
const index = binarySearch(nums, target);
|
||||
console.log(`目标元素 6 的索引 = ${index}`);
|
@ -0,0 +1,44 @@
|
||||
/**
|
||||
* File: build_tree.js
|
||||
* Created Time: 2023-07-30
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
const { TreeNode } = require('../modules/TreeNode');
|
||||
|
||||
/* 构建二叉树:分治 */
|
||||
function dfs(preorder, inorder, hmap, i, l, r) {
|
||||
// 子树区间为空时终止
|
||||
if (r - l < 0) return null;
|
||||
// 初始化根节点
|
||||
const root = new TreeNode(preorder[i]);
|
||||
// 查询 m ,从而划分左右子树
|
||||
const m = hmap.get(preorder[i]);
|
||||
// 子问题:构建左子树
|
||||
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1);
|
||||
// 子问题:构建右子树
|
||||
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r);
|
||||
// 返回根节点
|
||||
return root;
|
||||
}
|
||||
|
||||
/* 构建二叉树 */
|
||||
function buildTree(preorder, inorder) {
|
||||
// 初始化哈希表,存储 inorder 元素到索引的映射
|
||||
let hmap = new Map();
|
||||
for (let i = 0; i < inorder.length; i++) {
|
||||
hmap.set(inorder[i], i);
|
||||
}
|
||||
const root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1);
|
||||
return root;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const preorder = [3, 9, 2, 1, 7];
|
||||
const inorder = [9, 3, 1, 2, 7];
|
||||
console.log('前序遍历 = ' + JSON.stringify(preorder));
|
||||
console.log('中序遍历 = ' + JSON.stringify(inorder));
|
||||
const root = buildTree(preorder, inorder);
|
||||
console.log('构建的二叉树为:');
|
||||
printTree(root);
|
@ -0,0 +1,52 @@
|
||||
/**
|
||||
* File: hanota.js
|
||||
* Created Time: 2023-07-30
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* 移动一个圆盘 */
|
||||
function move(src, tar) {
|
||||
// 从 src 顶部拿出一个圆盘
|
||||
const pan = src.pop();
|
||||
// 将圆盘放入 tar 顶部
|
||||
tar.push(pan);
|
||||
}
|
||||
|
||||
/* 求解汉诺塔:问题 f(i) */
|
||||
function dfs(i, src, buf, tar) {
|
||||
// 若 src 只剩下一个圆盘,则直接将其移到 tar
|
||||
if (i === 1) {
|
||||
move(src, tar);
|
||||
return;
|
||||
}
|
||||
// 子问题 f(i-1) :将 src 顶部 i-1 个圆盘借助 tar 移到 buf
|
||||
dfs(i - 1, src, tar, buf);
|
||||
// 子问题 f(1) :将 src 剩余一个圆盘移到 tar
|
||||
move(src, tar);
|
||||
// 子问题 f(i-1) :将 buf 顶部 i-1 个圆盘借助 src 移到 tar
|
||||
dfs(i - 1, buf, src, tar);
|
||||
}
|
||||
|
||||
/* 求解汉诺塔 */
|
||||
function solveHanota(A, B, C) {
|
||||
const n = A.length;
|
||||
// 将 A 顶部 n 个圆盘借助 B 移到 C
|
||||
dfs(n, A, B, C);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
// 列表尾部是柱子顶部
|
||||
const A = [5, 4, 3, 2, 1];
|
||||
const B = [];
|
||||
const C = [];
|
||||
console.log('初始状态下:');
|
||||
console.log(`A = ${JSON.stringify(A)}`);
|
||||
console.log(`B = ${JSON.stringify(B)}`);
|
||||
console.log(`C = ${JSON.stringify(C)}`);
|
||||
|
||||
solveHanota(A, B, C);
|
||||
|
||||
console.log('圆盘移动完成后:');
|
||||
console.log(`A = ${JSON.stringify(A)}`);
|
||||
console.log(`B = ${JSON.stringify(B)}`);
|
||||
console.log(`C = ${JSON.stringify(C)}`);
|
@ -0,0 +1,54 @@
|
||||
/**
|
||||
* File: subset_sum_i.ts
|
||||
* Created Time: 2023-07-30
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* 回溯算法:子集和 I */
|
||||
function backtrack(
|
||||
state: number[],
|
||||
target: number,
|
||||
choices: number[],
|
||||
start: number,
|
||||
res: number[][]
|
||||
): void {
|
||||
// 子集和等于 target 时,记录解
|
||||
if (target === 0) {
|
||||
res.push([...state]);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
// 剪枝二:从 start 开始遍历,避免生成重复子集
|
||||
for (let i = start; i < choices.length; i++) {
|
||||
// 剪枝一:若子集和超过 target ,则直接结束循环
|
||||
// 这是因为数组已排序,后边元素更大,子集和一定超过 target
|
||||
if (target - choices[i] < 0) {
|
||||
break;
|
||||
}
|
||||
// 尝试:做出选择,更新 target, start
|
||||
state.push(choices[i]);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, target - choices[i], choices, i, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
state.pop();
|
||||
}
|
||||
}
|
||||
|
||||
/* 求解子集和 I */
|
||||
function subsetSumI(nums: number[], target: number): number[][] {
|
||||
const state = []; // 状态(子集)
|
||||
nums.sort(); // 对 nums 进行排序
|
||||
const start = 0; // 遍历起始点
|
||||
const res = []; // 结果列表(子集列表)
|
||||
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(`输入数组 nums = ${JSON.stringify(nums)}, target = ${target}`);
|
||||
console.log(`所有和等于 ${target} 的子集 res = ${JSON.stringify(res)}`);
|
||||
|
||||
export {};
|
@ -0,0 +1,59 @@
|
||||
/**
|
||||
* File: subset_sum_ii.ts
|
||||
* Created Time: 2023-07-30
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* 回溯算法:子集和 II */
|
||||
function backtrack(
|
||||
state: number[],
|
||||
target: number,
|
||||
choices: number[],
|
||||
start: number,
|
||||
res: number[][]
|
||||
): void {
|
||||
// 子集和等于 target 时,记录解
|
||||
if (target === 0) {
|
||||
res.push([...state]);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
// 剪枝二:从 start 开始遍历,避免生成重复子集
|
||||
// 剪枝三:从 start 开始遍历,避免重复选择同一元素
|
||||
for (let i = start; i < choices.length; i++) {
|
||||
// 剪枝一:若子集和超过 target ,则直接结束循环
|
||||
// 这是因为数组已排序,后边元素更大,子集和一定超过 target
|
||||
if (target - choices[i] < 0) {
|
||||
break;
|
||||
}
|
||||
// 剪枝四:如果该元素与左边元素相等,说明该搜索分支重复,直接跳过
|
||||
if (i > start && choices[i] === choices[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
// 尝试:做出选择,更新 target, start
|
||||
state.push(choices[i]);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, target - choices[i], choices, i + 1, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
state.pop();
|
||||
}
|
||||
}
|
||||
|
||||
/* 求解子集和 II */
|
||||
function subsetSumII(nums: number[], target: number): number[][] {
|
||||
const state = []; // 状态(子集)
|
||||
nums.sort(); // 对 nums 进行排序
|
||||
const start = 0; // 遍历起始点
|
||||
const res = []; // 结果列表(子集列表)
|
||||
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(`输入数组 nums = ${JSON.stringify(nums)}, target = ${target}`);
|
||||
console.log(`所有和等于 ${target} 的子集 res = ${JSON.stringify(res)}`);
|
||||
|
||||
export {};
|
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* File: binary_search_recur.ts
|
||||
* Created Time: 2023-07-30
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* 二分查找:问题 f(i, j) */
|
||||
function dfs(nums: number[], target: number, i: number, j: number): number {
|
||||
// 若区间为空,代表无目标元素,则返回 -1
|
||||
if (i > j) {
|
||||
return -1;
|
||||
}
|
||||
// 计算中点索引 m
|
||||
const m = i + ((j - i) >> 1);
|
||||
if (nums[m] < target) {
|
||||
// 递归子问题 f(m+1, j)
|
||||
return dfs(nums, target, m + 1, j);
|
||||
} else if (nums[m] > target) {
|
||||
// 递归子问题 f(i, m-1)
|
||||
return dfs(nums, target, i, m - 1);
|
||||
} else {
|
||||
// 找到目标元素,返回其索引
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/* 二分查找 */
|
||||
function binarySearch(nums: number[], target: number): number {
|
||||
const n = nums.length;
|
||||
// 求解问题 f(0, n-1)
|
||||
return dfs(nums, target, 0, n - 1);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const target = 6;
|
||||
const nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
|
||||
// 二分查找(双闭区间)
|
||||
const index = binarySearch(nums, target);
|
||||
console.log(`目标元素 6 的索引 = ${index}`);
|
||||
|
||||
export {};
|
@ -0,0 +1,51 @@
|
||||
/**
|
||||
* File: build_tree.ts
|
||||
* Created Time: 2023-07-30
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
import { printTree } from '../modules/PrintUtil';
|
||||
import { TreeNode } from '../modules/TreeNode';
|
||||
|
||||
/* 构建二叉树:分治 */
|
||||
function dfs(
|
||||
preorder: number[],
|
||||
inorder: number[],
|
||||
hmap: Map<number, number>,
|
||||
i: number,
|
||||
l: number,
|
||||
r: number
|
||||
): TreeNode | null {
|
||||
// 子树区间为空时终止
|
||||
if (r - l < 0) return null;
|
||||
// 初始化根节点
|
||||
const root: TreeNode = new TreeNode(preorder[i]);
|
||||
// 查询 m ,从而划分左右子树
|
||||
const m = hmap.get(preorder[i]);
|
||||
// 子问题:构建左子树
|
||||
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1);
|
||||
// 子问题:构建右子树
|
||||
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r);
|
||||
// 返回根节点
|
||||
return root;
|
||||
}
|
||||
|
||||
/* 构建二叉树 */
|
||||
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
|
||||
// 初始化哈希表,存储 inorder 元素到索引的映射
|
||||
let hmap = new Map<number, number>();
|
||||
for (let i = 0; i < inorder.length; i++) {
|
||||
hmap.set(inorder[i], i);
|
||||
}
|
||||
const root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1);
|
||||
return root;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const preorder = [3, 9, 2, 1, 7];
|
||||
const inorder = [9, 3, 1, 2, 7];
|
||||
console.log('前序遍历 = ' + JSON.stringify(preorder));
|
||||
console.log('中序遍历 = ' + JSON.stringify(inorder));
|
||||
const root = buildTree(preorder, inorder);
|
||||
console.log('构建的二叉树为:');
|
||||
printTree(root);
|
@ -0,0 +1,52 @@
|
||||
/**
|
||||
* File: hanota.ts
|
||||
* Created Time: 2023-07-30
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* 移动一个圆盘 */
|
||||
function move(src: number[], tar: number[]): void {
|
||||
// 从 src 顶部拿出一个圆盘
|
||||
const pan = src.pop();
|
||||
// 将圆盘放入 tar 顶部
|
||||
tar.push(pan);
|
||||
}
|
||||
|
||||
/* 求解汉诺塔:问题 f(i) */
|
||||
function dfs(i: number, src: number[], buf: number[], tar: number[]): void {
|
||||
// 若 src 只剩下一个圆盘,则直接将其移到 tar
|
||||
if (i === 1) {
|
||||
move(src, tar);
|
||||
return;
|
||||
}
|
||||
// 子问题 f(i-1) :将 src 顶部 i-1 个圆盘借助 tar 移到 buf
|
||||
dfs(i - 1, src, tar, buf);
|
||||
// 子问题 f(1) :将 src 剩余一个圆盘移到 tar
|
||||
move(src, tar);
|
||||
// 子问题 f(i-1) :将 buf 顶部 i-1 个圆盘借助 src 移到 tar
|
||||
dfs(i - 1, buf, src, tar);
|
||||
}
|
||||
|
||||
/* 求解汉诺塔 */
|
||||
function solveHanota(A: number[], B: number[], C: number[]): void {
|
||||
const n = A.length;
|
||||
// 将 A 顶部 n 个圆盘借助 B 移到 C
|
||||
dfs(n, A, B, C);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
// 列表尾部是柱子顶部
|
||||
const A = [5, 4, 3, 2, 1];
|
||||
const B = [];
|
||||
const C = [];
|
||||
console.log('初始状态下:');
|
||||
console.log(`A = ${JSON.stringify(A)}`);
|
||||
console.log(`B = ${JSON.stringify(B)}`);
|
||||
console.log(`C = ${JSON.stringify(C)}`);
|
||||
|
||||
solveHanota(A, B, C);
|
||||
|
||||
console.log('圆盘移动完成后:');
|
||||
console.log(`A = ${JSON.stringify(A)}`);
|
||||
console.log(`B = ${JSON.stringify(B)}`);
|
||||
console.log(`C = ${JSON.stringify(C)}`);
|
Loading…
Reference in new issue