diff --git a/codes/javascript/chapter_array_and_linkedlist/array.js b/codes/javascript/chapter_array_and_linkedlist/array.js index a8063fbe5..8781d312d 100644 --- a/codes/javascript/chapter_array_and_linkedlist/array.js +++ b/codes/javascript/chapter_array_and_linkedlist/array.js @@ -61,7 +61,7 @@ function traverse(nums) { /* 在数组中查找指定元素 */ function find(nums, target) { for (let i = 0; i < nums.length; i++) { - if (nums[i] == target) return i; + if (nums[i] === target) return i; } return -1; } diff --git a/codes/javascript/chapter_backtracking/subset_sum_i.js b/codes/javascript/chapter_backtracking/subset_sum_i.js new file mode 100644 index 000000000..d07c85a2c --- /dev/null +++ b/codes/javascript/chapter_backtracking/subset_sum_i.js @@ -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)}`); diff --git a/codes/javascript/chapter_backtracking/subset_sum_i_naive.js b/codes/javascript/chapter_backtracking/subset_sum_i_naive.js new file mode 100644 index 000000000..16d9b1dc4 --- /dev/null +++ b/codes/javascript/chapter_backtracking/subset_sum_i_naive.js @@ -0,0 +1,44 @@ +/** + * 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('请注意,该方法输出的结果包含重复集合'); diff --git a/codes/javascript/chapter_backtracking/subset_sum_ii.js b/codes/javascript/chapter_backtracking/subset_sum_ii.js new file mode 100644 index 000000000..293b468e6 --- /dev/null +++ b/codes/javascript/chapter_backtracking/subset_sum_ii.js @@ -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)}`); diff --git a/codes/javascript/chapter_computational_complexity/time_complexity.js b/codes/javascript/chapter_computational_complexity/time_complexity.js index 78c124361..6f455f9ff 100644 --- a/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -77,7 +77,7 @@ function exponential(n) { /* 指数阶(递归实现) */ function expRecur(n) { - if (n == 1) return 1; + if (n === 1) return 1; return expRecur(n - 1) + expRecur(n - 1) + 1; } @@ -109,7 +109,7 @@ function linearLogRecur(n) { /* 阶乘阶(递归实现) */ function factorialRecur(n) { - if (n == 0) return 1; + if (n === 0) return 1; let count = 0; // 从 1 个分裂出 n 个 for (let i = 0; i < n; i++) { diff --git a/codes/javascript/chapter_divide_and_conquer/binary_search_recur.js b/codes/javascript/chapter_divide_and_conquer/binary_search_recur.js new file mode 100644 index 000000000..699caae5b --- /dev/null +++ b/codes/javascript/chapter_divide_and_conquer/binary_search_recur.js @@ -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}`); diff --git a/codes/javascript/chapter_divide_and_conquer/build_tree.js b/codes/javascript/chapter_divide_and_conquer/build_tree.js new file mode 100644 index 000000000..67100bf58 --- /dev/null +++ b/codes/javascript/chapter_divide_and_conquer/build_tree.js @@ -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); diff --git a/codes/javascript/chapter_divide_and_conquer/hanota.js b/codes/javascript/chapter_divide_and_conquer/hanota.js new file mode 100644 index 000000000..009d43253 --- /dev/null +++ b/codes/javascript/chapter_divide_and_conquer/hanota.js @@ -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)}`); diff --git a/codes/javascript/chapter_dynamic_programming/climbing_stairs_dfs.js b/codes/javascript/chapter_dynamic_programming/climbing_stairs_dfs.js index 5e9cef34d..f93d0f668 100644 --- a/codes/javascript/chapter_dynamic_programming/climbing_stairs_dfs.js +++ b/codes/javascript/chapter_dynamic_programming/climbing_stairs_dfs.js @@ -7,7 +7,7 @@ /* 搜索 */ function dfs(i) { // 已知 dp[1] 和 dp[2] ,返回之 - if (i == 1 || i == 2) return i; + if (i === 1 || i === 2) return i; // dp[i] = dp[i-1] + dp[i-2] const count = dfs(i - 1) + dfs(i - 2); return count; diff --git a/codes/javascript/chapter_dynamic_programming/climbing_stairs_dfs_mem.js b/codes/javascript/chapter_dynamic_programming/climbing_stairs_dfs_mem.js index 17860ed7d..5986a5461 100644 --- a/codes/javascript/chapter_dynamic_programming/climbing_stairs_dfs_mem.js +++ b/codes/javascript/chapter_dynamic_programming/climbing_stairs_dfs_mem.js @@ -7,7 +7,7 @@ /* 记忆化搜索 */ function dfs(i, mem) { // 已知 dp[1] 和 dp[2] ,返回之 - if (i == 1 || i == 2) return i; + if (i === 1 || i === 2) return i; // 若存在记录 dp[i] ,则直接返回之 if (mem[i] != -1) return mem[i]; // dp[i] = dp[i-1] + dp[i-2] diff --git a/codes/javascript/chapter_dynamic_programming/climbing_stairs_dp.js b/codes/javascript/chapter_dynamic_programming/climbing_stairs_dp.js index fd0ccc9d7..1bd725137 100644 --- a/codes/javascript/chapter_dynamic_programming/climbing_stairs_dp.js +++ b/codes/javascript/chapter_dynamic_programming/climbing_stairs_dp.js @@ -6,7 +6,7 @@ /* 爬楼梯:动态规划 */ function climbingStairsDP(n) { - if (n == 1 || n == 2) return n; + if (n === 1 || n === 2) return n; // 初始化 dp 表,用于存储子问题的解 const dp = new Array(n + 1).fill(-1); // 初始状态:预设最小子问题的解 @@ -21,7 +21,7 @@ function climbingStairsDP(n) { /* 爬楼梯:状态压缩后的动态规划 */ function climbingStairsDPComp(n) { - if (n == 1 || n == 2) return n; + if (n === 1 || n === 2) return n; let a = 1, b = 2; for (let i = 3; i <= n; i++) { diff --git a/codes/javascript/chapter_graph/graph_adjacency_matrix.js b/codes/javascript/chapter_graph/graph_adjacency_matrix.js index 216680934..fb07086d5 100644 --- a/codes/javascript/chapter_graph/graph_adjacency_matrix.js +++ b/codes/javascript/chapter_graph/graph_adjacency_matrix.js @@ -69,7 +69,7 @@ class GraphAdjMat { if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) { throw new RangeError('Index Out Of Bounds Exception'); } - // 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i) + // 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) === (j, i) this.adjMat[i][j] = 1; this.adjMat[j][i] = 1; } diff --git a/codes/javascript/chapter_heap/my_heap.js b/codes/javascript/chapter_heap/my_heap.js index fefb8e066..52e521383 100644 --- a/codes/javascript/chapter_heap/my_heap.js +++ b/codes/javascript/chapter_heap/my_heap.js @@ -103,7 +103,7 @@ class MaxHeap { if (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l; if (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r; // 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出 - if (ma == i) break; + if (ma === i) break; // 交换两节点 this.#swap(i, ma); // 循环向下堆化 diff --git a/codes/javascript/chapter_searching/binary_search_edge.js b/codes/javascript/chapter_searching/binary_search_edge.js index 515c75b66..4c7aadb9a 100644 --- a/codes/javascript/chapter_searching/binary_search_edge.js +++ b/codes/javascript/chapter_searching/binary_search_edge.js @@ -18,7 +18,7 @@ function binarySearchLeftEdge(nums, target) { j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中 } } - if (i == nums.length || nums[i] != target) { + if (i === nums.length || nums[i] != target) { return -1; // 未找到目标元素,返回 -1 } return i; diff --git a/codes/javascript/chapter_stack_and_queue/array_queue.js b/codes/javascript/chapter_stack_and_queue/array_queue.js index c3786af73..a57fbf4a4 100644 --- a/codes/javascript/chapter_stack_and_queue/array_queue.js +++ b/codes/javascript/chapter_stack_and_queue/array_queue.js @@ -26,12 +26,12 @@ class ArrayQueue { /* 判断队列是否为空 */ empty() { - return this.#queSize == 0; + return this.#queSize === 0; } /* 入队 */ push(num) { - if (this.size == this.capacity) { + if (this.size === this.capacity) { console.log('队列已满'); return; } diff --git a/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js b/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js index f0ccee0cd..c5f9a53ac 100644 --- a/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js +++ b/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js @@ -22,7 +22,7 @@ class LinkedListStack { /* 判断栈是否为空 */ isEmpty() { - return this.size == 0; + return this.size === 0; } /* 入栈 */ diff --git a/codes/typescript/chapter_backtracking/subset_sum_i.ts b/codes/typescript/chapter_backtracking/subset_sum_i.ts new file mode 100644 index 000000000..b9e2497dd --- /dev/null +++ b/codes/typescript/chapter_backtracking/subset_sum_i.ts @@ -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 {}; diff --git a/codes/typescript/chapter_backtracking/subset_sum_i_naive.ts b/codes/typescript/chapter_backtracking/subset_sum_i_naive.ts new file mode 100644 index 000000000..5a3d8248f --- /dev/null +++ b/codes/typescript/chapter_backtracking/subset_sum_i_naive.ts @@ -0,0 +1,52 @@ +/** + * File: subset_sum_i_naive.ts + * Created Time: 2023-07-30 + * Author: yuan0221 (yl1452491917@gmail.com) + */ + +/* 回溯算法:子集和 I */ +function backtrack( + state: number[], + target: number, + total: number, + choices: number[], + res: number[][] +): void { + // 子集和等于 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: number[], target: number): number[][] { + 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('请注意,该方法输出的结果包含重复集合'); + +export {}; diff --git a/codes/typescript/chapter_backtracking/subset_sum_ii.ts b/codes/typescript/chapter_backtracking/subset_sum_ii.ts new file mode 100644 index 000000000..ae010e65c --- /dev/null +++ b/codes/typescript/chapter_backtracking/subset_sum_ii.ts @@ -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 {}; diff --git a/codes/typescript/chapter_computational_complexity/time_complexity.ts b/codes/typescript/chapter_computational_complexity/time_complexity.ts index dfd4d2c57..4acdb0a5d 100644 --- a/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -77,7 +77,7 @@ function exponential(n: number): number { /* 指数阶(递归实现) */ function expRecur(n: number): number { - if (n == 1) return 1; + if (n === 1) return 1; return expRecur(n - 1) + expRecur(n - 1) + 1; } @@ -109,7 +109,7 @@ function linearLogRecur(n: number): number { /* 阶乘阶(递归实现) */ function factorialRecur(n: number): number { - if (n == 0) return 1; + if (n === 0) return 1; let count = 0; // 从 1 个分裂出 n 个 for (let i = 0; i < n; i++) { diff --git a/codes/typescript/chapter_divide_and_conquer/binary_search_recur.ts b/codes/typescript/chapter_divide_and_conquer/binary_search_recur.ts new file mode 100644 index 000000000..233c6a501 --- /dev/null +++ b/codes/typescript/chapter_divide_and_conquer/binary_search_recur.ts @@ -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 {}; diff --git a/codes/typescript/chapter_divide_and_conquer/build_tree.ts b/codes/typescript/chapter_divide_and_conquer/build_tree.ts new file mode 100644 index 000000000..69f4afa04 --- /dev/null +++ b/codes/typescript/chapter_divide_and_conquer/build_tree.ts @@ -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, + 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(); + 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); diff --git a/codes/typescript/chapter_divide_and_conquer/hanota.ts b/codes/typescript/chapter_divide_and_conquer/hanota.ts new file mode 100644 index 000000000..e5f0c55fe --- /dev/null +++ b/codes/typescript/chapter_divide_and_conquer/hanota.ts @@ -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)}`); diff --git a/codes/typescript/chapter_dynamic_programming/climbing_stairs_dfs.ts b/codes/typescript/chapter_dynamic_programming/climbing_stairs_dfs.ts index 7f6de3ff4..36f71088f 100644 --- a/codes/typescript/chapter_dynamic_programming/climbing_stairs_dfs.ts +++ b/codes/typescript/chapter_dynamic_programming/climbing_stairs_dfs.ts @@ -7,7 +7,7 @@ /* 搜索 */ function dfs(i: number): number { // 已知 dp[1] 和 dp[2] ,返回之 - if (i == 1 || i == 2) return i; + if (i === 1 || i === 2) return i; // dp[i] = dp[i-1] + dp[i-2] const count = dfs(i - 1) + dfs(i - 2); return count; diff --git a/codes/typescript/chapter_dynamic_programming/climbing_stairs_dfs_mem.ts b/codes/typescript/chapter_dynamic_programming/climbing_stairs_dfs_mem.ts index 0820e8b5f..13d0d6fe2 100644 --- a/codes/typescript/chapter_dynamic_programming/climbing_stairs_dfs_mem.ts +++ b/codes/typescript/chapter_dynamic_programming/climbing_stairs_dfs_mem.ts @@ -7,7 +7,7 @@ /* 记忆化搜索 */ function dfs(i: number, mem: number[]): number { // 已知 dp[1] 和 dp[2] ,返回之 - if (i == 1 || i == 2) return i; + if (i === 1 || i === 2) return i; // 若存在记录 dp[i] ,则直接返回之 if (mem[i] != -1) return mem[i]; // dp[i] = dp[i-1] + dp[i-2] diff --git a/codes/typescript/chapter_dynamic_programming/climbing_stairs_dp.ts b/codes/typescript/chapter_dynamic_programming/climbing_stairs_dp.ts index 8dfdfb33f..313b2f622 100644 --- a/codes/typescript/chapter_dynamic_programming/climbing_stairs_dp.ts +++ b/codes/typescript/chapter_dynamic_programming/climbing_stairs_dp.ts @@ -6,7 +6,7 @@ /* 爬楼梯:动态规划 */ function climbingStairsDP(n: number): number { - if (n == 1 || n == 2) return n; + if (n === 1 || n === 2) return n; // 初始化 dp 表,用于存储子问题的解 const dp = new Array(n + 1).fill(-1); // 初始状态:预设最小子问题的解 @@ -21,7 +21,7 @@ function climbingStairsDP(n: number): number { /* 爬楼梯:状态压缩后的动态规划 */ function climbingStairsDPComp(n: number): number { - if (n == 1 || n == 2) return n; + if (n === 1 || n === 2) return n; let a = 1, b = 2; for (let i = 3; i <= n; i++) { diff --git a/codes/typescript/chapter_graph/graph_adjacency_matrix.ts b/codes/typescript/chapter_graph/graph_adjacency_matrix.ts index eb074cdd7..418e9b000 100644 --- a/codes/typescript/chapter_graph/graph_adjacency_matrix.ts +++ b/codes/typescript/chapter_graph/graph_adjacency_matrix.ts @@ -69,7 +69,7 @@ class GraphAdjMat { if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) { throw new RangeError('Index Out Of Bounds Exception'); } - // 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i) + // 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) === (j, i) this.adjMat[i][j] = 1; this.adjMat[j][i] = 1; } diff --git a/codes/typescript/chapter_heap/my_heap.ts b/codes/typescript/chapter_heap/my_heap.ts index 72d95541e..ed2e9b209 100644 --- a/codes/typescript/chapter_heap/my_heap.ts +++ b/codes/typescript/chapter_heap/my_heap.ts @@ -102,7 +102,7 @@ class MaxHeap { if (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l; if (r < this.size() && this.maxHeap[r] > this.maxHeap[ma]) ma = r; // 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出 - if (ma == i) break; + if (ma === i) break; // 交换两节点 this.swap(i, ma); // 循环向下堆化 diff --git a/codes/typescript/chapter_searching/binary_search_edge.ts b/codes/typescript/chapter_searching/binary_search_edge.ts index 95f819c60..a47bee09d 100644 --- a/codes/typescript/chapter_searching/binary_search_edge.ts +++ b/codes/typescript/chapter_searching/binary_search_edge.ts @@ -17,7 +17,7 @@ function binarySearchLeftEdge(nums: number[], target: number): number { j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中 } } - if (i == nums.length || nums[i] != target) { + if (i === nums.length || nums[i] != target) { return -1; // 未找到目标元素,返回 -1 } return i; diff --git a/codes/typescript/chapter_stack_and_queue/array_queue.ts b/codes/typescript/chapter_stack_and_queue/array_queue.ts index 62941afa2..d8898b12f 100644 --- a/codes/typescript/chapter_stack_and_queue/array_queue.ts +++ b/codes/typescript/chapter_stack_and_queue/array_queue.ts @@ -27,12 +27,12 @@ class ArrayQueue { /* 判断队列是否为空 */ empty(): boolean { - return this.queSize == 0; + return this.queSize === 0; } /* 入队 */ push(num: number): void { - if (this.size == this.capacity) { + if (this.size === this.capacity) { console.log('队列已满'); return; } diff --git a/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts b/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts index 2b5517f8a..9b8f0617e 100644 --- a/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts +++ b/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts @@ -22,7 +22,7 @@ class LinkedListStack { /* 判断栈是否为空 */ isEmpty(): boolean { - return this.size == 0; + return this.size === 0; } /* 入栈 */