From 978857570fa15dff2264a8d33c453da7449e2da9 Mon Sep 17 00:00:00 2001 From: krahets Date: Sat, 2 Sep 2023 23:07:47 +0800 Subject: [PATCH] Format JS and TS code. --- .../time_complexity.js | 2 +- .../climbing_stairs_backtrack.js | 1 - .../climbing_stairs_dp.js | 1 - .../chapter_dynamic_programming/knapsack.js | 3 ++- .../chapter_dynamic_programming/min_path_sum.js | 4 ++-- .../chapter_greedy/coin_change_greedy.js | 12 ++++++------ .../chapter_greedy/fractional_knapsack.js | 6 +++--- codes/javascript/chapter_greedy/max_capacity.js | 7 ++++--- .../chapter_greedy/max_product_cutting.js | 4 ++-- codes/javascript/chapter_heap/my_heap.js | 1 - codes/javascript/chapter_heap/top_k.js | 4 ++-- .../chapter_searching/binary_search_insertion.js | 4 ++-- codes/javascript/chapter_searching/two_sum.js | 2 +- codes/javascript/chapter_sorting/bubble_sort.js | 4 ++-- .../chapter_computational_complexity/iteration.rs | 2 +- .../chapter_array_and_linkedlist/array.ts | 2 +- codes/typescript/chapter_backtracking/n_queens.ts | 2 +- .../chapter_computational_complexity/iteration.ts | 2 +- .../chapter_computational_complexity/recursion.ts | 2 +- .../time_complexity.ts | 2 +- .../climbing_stairs_backtrack.ts | 2 +- .../climbing_stairs_constraint_dp.ts | 5 +---- .../chapter_dynamic_programming/knapsack.ts | 3 +-- .../chapter_greedy/coin_change_greedy.ts | 14 +++++++------- .../chapter_greedy/fractional_knapsack.ts | 8 ++++---- codes/typescript/chapter_greedy/max_capacity.ts | 9 +++++---- .../chapter_greedy/max_product_cutting.ts | 6 +++--- codes/typescript/chapter_heap/top_k.ts | 4 ++-- .../chapter_searching/binary_search_edge.ts | 3 +-- .../chapter_searching/binary_search_insertion.ts | 5 ++++- codes/typescript/chapter_sorting/bubble_sort.ts | 4 ++-- .../typescript/chapter_tree/binary_search_tree.ts | 7 +++++-- docs/chapter_backtracking/permutations_problem.md | 2 +- docs/chapter_hashing/hash_algorithm.md | 4 ++-- docs/chapter_tree/binary_search_tree.md | 6 +++--- 35 files changed, 75 insertions(+), 74 deletions(-) diff --git a/codes/javascript/chapter_computational_complexity/time_complexity.js b/codes/javascript/chapter_computational_complexity/time_complexity.js index bea5d2d0..938f860a 100644 --- a/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -46,7 +46,7 @@ function bubbleSort(nums) { let count = 0; // 计数器 // 外循环:未排序区间为 [0, i] for (let i = nums.length - 1; i > 0; i--) { - // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (let j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/javascript/chapter_dynamic_programming/climbing_stairs_backtrack.js b/codes/javascript/chapter_dynamic_programming/climbing_stairs_backtrack.js index 96c82860..90eef731 100644 --- a/codes/javascript/chapter_dynamic_programming/climbing_stairs_backtrack.js +++ b/codes/javascript/chapter_dynamic_programming/climbing_stairs_backtrack.js @@ -32,4 +32,3 @@ function climbingStairsBacktrack(n) { const n = 9; const res = climbingStairsBacktrack(n); console.log(`爬 ${n} 阶楼梯共有 ${res} 种方案`); - diff --git a/codes/javascript/chapter_dynamic_programming/climbing_stairs_dp.js b/codes/javascript/chapter_dynamic_programming/climbing_stairs_dp.js index 6890ed91..cb5d37c5 100644 --- a/codes/javascript/chapter_dynamic_programming/climbing_stairs_dp.js +++ b/codes/javascript/chapter_dynamic_programming/climbing_stairs_dp.js @@ -38,4 +38,3 @@ let res = climbingStairsDP(n); console.log(`爬 ${n} 阶楼梯共有 ${res} 种方案`); res = climbingStairsDPComp(n); console.log(`爬 ${n} 阶楼梯共有 ${res} 种方案`); - diff --git a/codes/javascript/chapter_dynamic_programming/knapsack.js b/codes/javascript/chapter_dynamic_programming/knapsack.js index 885fc54d..7a8549e0 100644 --- a/codes/javascript/chapter_dynamic_programming/knapsack.js +++ b/codes/javascript/chapter_dynamic_programming/knapsack.js @@ -37,7 +37,8 @@ function knapsackDFSMem(wgt, val, mem, i, c) { } // 计算不放入和放入物品 i 的最大价值 const no = knapsackDFSMem(wgt, val, mem, i - 1, c); - const yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]; + const yes = + knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]; // 记录并返回两种方案中价值更大的那一个 mem[i][c] = Math.max(no, yes); return mem[i][c]; diff --git a/codes/javascript/chapter_dynamic_programming/min_path_sum.js b/codes/javascript/chapter_dynamic_programming/min_path_sum.js index 7f6a473b..b130ebc8 100644 --- a/codes/javascript/chapter_dynamic_programming/min_path_sum.js +++ b/codes/javascript/chapter_dynamic_programming/min_path_sum.js @@ -98,7 +98,7 @@ const grid = [ [2, 2, 4, 2], [5, 3, 2, 1], [4, 3, 5, 2], -] +]; const n = grid.length, m = grid[0].length; // 暴力搜索 @@ -118,4 +118,4 @@ console.log(`从左上角到右下角的最小路径和为 ${res}`); // 状态压缩后的动态规划 res = minPathSumDPComp(grid); -console.log(`从左上角到右下角的最小路径和为 ${res}`); \ No newline at end of file +console.log(`从左上角到右下角的最小路径和为 ${res}`); diff --git a/codes/javascript/chapter_greedy/coin_change_greedy.js b/codes/javascript/chapter_greedy/coin_change_greedy.js index 4be23dfe..2d80ddad 100644 --- a/codes/javascript/chapter_greedy/coin_change_greedy.js +++ b/codes/javascript/chapter_greedy/coin_change_greedy.js @@ -5,7 +5,7 @@ */ /* 零钱兑换:贪心 */ -function coin_change_greedy(coins, amt) { +function coinChangeGreedy(coins, amt) { // 假设 coins 数组有序 let i = coins.length - 1; let count = 0; @@ -27,22 +27,22 @@ function coin_change_greedy(coins, amt) { // 贪心:能够保证找到全局最优解 let coins = [1, 5, 10, 20, 50, 100]; let amt = 186; -let res = coin_change_greedy(coins, amt); +let res = coinChangeGreedy(coins, amt); console.log(`\ncoins = ${coins}, amt = ${amt}`); console.log(`凑到 ${amt} 所需的最少硬币数量为 ${res}`); // 贪心:无法保证找到全局最优解 coins = [1, 20, 50]; amt = 60; -res = coin_change_greedy(coins, amt); +res = coinChangeGreedy(coins, amt); console.log(`\ncoins = ${coins}, amt = ${amt}`); console.log(`凑到 ${amt} 所需的最少硬币数量为 ${res}`); -console.log("实际上需要的最少数量为 3 ,即 20 + 20 + 20"); +console.log('实际上需要的最少数量为 3 ,即 20 + 20 + 20'); // 贪心:无法保证找到全局最优解 coins = [1, 49, 50]; amt = 98; -res = coin_change_greedy(coins, amt); +res = coinChangeGreedy(coins, amt); console.log(`\ncoins = ${coins}, amt = ${amt}`); console.log(`凑到 ${amt} 所需的最少硬币数量为 ${res}`); -console.log("实际上需要的最少数量为 2 ,即 49 + 49"); +console.log('实际上需要的最少数量为 2 ,即 49 + 49'); diff --git a/codes/javascript/chapter_greedy/fractional_knapsack.js b/codes/javascript/chapter_greedy/fractional_knapsack.js index a09b19a7..67e499c3 100644 --- a/codes/javascript/chapter_greedy/fractional_knapsack.js +++ b/codes/javascript/chapter_greedy/fractional_knapsack.js @@ -13,11 +13,11 @@ class Item { } /* 分数背包:贪心 */ -function fractional_knapsack(wgt, val, cap) { +function fractionalKnapsack(wgt, val, cap) { // 创建物品列表,包含两个属性:重量、价值 const items = wgt.map((w, i) => new Item(w, val[i])); // 按照单位价值 item.v / item.w 从高到低进行排序 - items.sort((a, b) => (b.v / b.w) - (a.v / a.w)); + items.sort((a, b) => b.v / b.w - a.v / a.w); // 循环贪心选择 let res = 0; for (const item of items) { @@ -42,5 +42,5 @@ const cap = 50; const n = wgt.length; // 贪心算法 -const res = fractional_knapsack(wgt, val, cap); +const res = fractionalKnapsack(wgt, val, cap); console.log(`不超过背包容量的最大物品价值为 ${res}`); diff --git a/codes/javascript/chapter_greedy/max_capacity.js b/codes/javascript/chapter_greedy/max_capacity.js index 07858f3d..95461bfe 100644 --- a/codes/javascript/chapter_greedy/max_capacity.js +++ b/codes/javascript/chapter_greedy/max_capacity.js @@ -5,9 +5,10 @@ */ /* 最大容量:贪心 */ -function max_capacity(ht) { +function maxCapacity(ht) { // 初始化 i, j 分列数组两端 - let i = 0, j = ht.length - 1; + let i = 0, + j = ht.length - 1; // 初始最大容量为 0 let res = 0; // 循环贪心选择,直至两板相遇 @@ -29,5 +30,5 @@ function max_capacity(ht) { const ht = [3, 8, 5, 2, 7, 7, 3, 4]; // 贪心算法 -const res = max_capacity(ht); +const res = maxCapacity(ht); console.log(`最大容量为 ${res}`); diff --git a/codes/javascript/chapter_greedy/max_product_cutting.js b/codes/javascript/chapter_greedy/max_product_cutting.js index e2c6bf6e..c222eeb5 100644 --- a/codes/javascript/chapter_greedy/max_product_cutting.js +++ b/codes/javascript/chapter_greedy/max_product_cutting.js @@ -5,7 +5,7 @@ */ /* 最大切分乘积:贪心 */ -function max_product_cutting(n) { +function maxProductCutting(n) { // 当 n <= 3 时,必须切分出一个 1 if (n <= 3) { return 1 * (n - 1); @@ -29,5 +29,5 @@ function max_product_cutting(n) { let n = 58; // 贪心算法 -let res = max_product_cutting(n); +let res = maxProductCutting(n); console.log(`最大切分乘积为 ${res}`); diff --git a/codes/javascript/chapter_heap/my_heap.js b/codes/javascript/chapter_heap/my_heap.js index 6ef8c0dd..37e089a8 100644 --- a/codes/javascript/chapter_heap/my_heap.js +++ b/codes/javascript/chapter_heap/my_heap.js @@ -151,7 +151,6 @@ console.log(`\n堆元素数量为 ${size}`); let isEmpty = maxHeap.isEmpty(); console.log(`\n堆是否为空 ${isEmpty}`); - module.exports = { MaxHeap, }; diff --git a/codes/javascript/chapter_heap/top_k.js b/codes/javascript/chapter_heap/top_k.js index 8d8ced42..700337fd 100644 --- a/codes/javascript/chapter_heap/top_k.js +++ b/codes/javascript/chapter_heap/top_k.js @@ -9,7 +9,7 @@ const { MaxHeap } = require('./my_heap'); /* 基于堆查找数组中最大的 k 个元素 */ function top_k_heap(nums, k) { // 使用大顶堆 MaxHeap,对数组 nums 取相反数 - const invertedNums = nums.map(num => -num); + const invertedNums = nums.map((num) => -num); // 将数组的前 k 个元素入堆 const heap = new MaxHeap(invertedNums.slice(0, k)); // 从第 k+1 个元素开始,保持堆的长度为 k @@ -23,7 +23,7 @@ function top_k_heap(nums, k) { // 取出堆中元素 const maxHeap = heap.getMaxHeap(); // 对堆中元素取相反数 - const invertedMaxHeap = maxHeap.map(num => -num); + const invertedMaxHeap = maxHeap.map((num) => -num); return invertedMaxHeap; } diff --git a/codes/javascript/chapter_searching/binary_search_insertion.js b/codes/javascript/chapter_searching/binary_search_insertion.js index 994f7f77..af654421 100644 --- a/codes/javascript/chapter_searching/binary_search_insertion.js +++ b/codes/javascript/chapter_searching/binary_search_insertion.js @@ -60,5 +60,5 @@ for (const target of [2, 6, 20]) { } module.exports = { - binarySearchInsertion -}; \ No newline at end of file + binarySearchInsertion, +}; diff --git a/codes/javascript/chapter_searching/two_sum.js b/codes/javascript/chapter_searching/two_sum.js index 4e47a41e..6b60445f 100644 --- a/codes/javascript/chapter_searching/two_sum.js +++ b/codes/javascript/chapter_searching/two_sum.js @@ -25,7 +25,7 @@ function twoSumHashTable(nums, target) { // 单层循环,时间复杂度 O(n) for (let i = 0; i < nums.length; i++) { if (m[target - nums[i]] !== undefined) { - return [m[target-nums[i]], i]; + return [m[target - nums[i]], i]; } else { m[nums[i]] = i; } diff --git a/codes/javascript/chapter_sorting/bubble_sort.js b/codes/javascript/chapter_sorting/bubble_sort.js index 0a4f2f7d..2c952696 100644 --- a/codes/javascript/chapter_sorting/bubble_sort.js +++ b/codes/javascript/chapter_sorting/bubble_sort.js @@ -8,7 +8,7 @@ function bubbleSort(nums) { // 外循环:未排序区间为 [0, i] for (let i = nums.length - 1; i > 0; i--) { - // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (let j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] @@ -25,7 +25,7 @@ function bubbleSortWithFlag(nums) { // 外循环:未排序区间为 [0, i] for (let i = nums.length - 1; i > 0; i--) { let flag = false; // 初始化标志位 - // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (let j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/rust/chapter_computational_complexity/iteration.rs b/codes/rust/chapter_computational_complexity/iteration.rs index 7f2b7d93..40326e68 100644 --- a/codes/rust/chapter_computational_complexity/iteration.rs +++ b/codes/rust/chapter_computational_complexity/iteration.rs @@ -5,7 +5,7 @@ */ -/* for 循环 */ +/* for 循环 */ fn for_loop(n: i32) -> i32 { let mut res = 0; // 循环求和 1, 2, ..., n-1, n diff --git a/codes/typescript/chapter_array_and_linkedlist/array.ts b/codes/typescript/chapter_array_and_linkedlist/array.ts index 09ebf4c8..eb5475e3 100644 --- a/codes/typescript/chapter_array_and_linkedlist/array.ts +++ b/codes/typescript/chapter_array_and_linkedlist/array.ts @@ -98,4 +98,4 @@ traverse(nums); let index = find(nums, 3); console.log('在 nums 中查找元素 3 ,得到索引 =', index); -export { }; +export {}; diff --git a/codes/typescript/chapter_backtracking/n_queens.ts b/codes/typescript/chapter_backtracking/n_queens.ts index 3aab291d..acbe1619 100644 --- a/codes/typescript/chapter_backtracking/n_queens.ts +++ b/codes/typescript/chapter_backtracking/n_queens.ts @@ -62,4 +62,4 @@ res.forEach((state) => { state.forEach((row) => console.log(row)); }); -export {}; \ No newline at end of file +export {}; diff --git a/codes/typescript/chapter_computational_complexity/iteration.ts b/codes/typescript/chapter_computational_complexity/iteration.ts index 8ecd6d8a..ee861fad 100644 --- a/codes/typescript/chapter_computational_complexity/iteration.ts +++ b/codes/typescript/chapter_computational_complexity/iteration.ts @@ -69,4 +69,4 @@ console.log(`while 循环(两次更新)求和结果 res = ${res}`); const resStr = nestedForLoop(n); console.log(`双层 for 循环的遍历结果 ${resStr}`); -export {}; \ No newline at end of file +export {}; diff --git a/codes/typescript/chapter_computational_complexity/recursion.ts b/codes/typescript/chapter_computational_complexity/recursion.ts index eb0246eb..88e14715 100644 --- a/codes/typescript/chapter_computational_complexity/recursion.ts +++ b/codes/typescript/chapter_computational_complexity/recursion.ts @@ -45,4 +45,4 @@ console.log(`尾递归函数的求和结果 res = ${res}`); res = fib(n); console.log(`斐波那契数列的第 ${n} 项为 ${res}`); -export {}; \ No newline at end of file +export {}; diff --git a/codes/typescript/chapter_computational_complexity/time_complexity.ts b/codes/typescript/chapter_computational_complexity/time_complexity.ts index 9f4f4ed3..f99d393d 100644 --- a/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -46,7 +46,7 @@ function bubbleSort(nums: number[]): number { let count = 0; // 计数器 // 外循环:未排序区间为 [0, i] for (let i = nums.length - 1; i > 0; i--) { - // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (let j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/typescript/chapter_dynamic_programming/climbing_stairs_backtrack.ts b/codes/typescript/chapter_dynamic_programming/climbing_stairs_backtrack.ts index 32368063..2e49885c 100644 --- a/codes/typescript/chapter_dynamic_programming/climbing_stairs_backtrack.ts +++ b/codes/typescript/chapter_dynamic_programming/climbing_stairs_backtrack.ts @@ -38,4 +38,4 @@ const n = 9; const res = climbingStairsBacktrack(n); console.log(`爬 ${n} 阶楼梯共有 ${res} 种方案`); -export { }; +export {}; diff --git a/codes/typescript/chapter_dynamic_programming/climbing_stairs_constraint_dp.ts b/codes/typescript/chapter_dynamic_programming/climbing_stairs_constraint_dp.ts index df38263e..85af4ad8 100644 --- a/codes/typescript/chapter_dynamic_programming/climbing_stairs_constraint_dp.ts +++ b/codes/typescript/chapter_dynamic_programming/climbing_stairs_constraint_dp.ts @@ -10,10 +10,7 @@ function climbingStairsConstraintDP(n: number): number { return 1; } // 初始化 dp 表,用于存储子问题的解 - const dp = Array.from( - { length: n + 1 }, - () => new Array(3) - ); + const dp = Array.from({ length: n + 1 }, () => new Array(3)); // 初始状态:预设最小子问题的解 dp[1][1] = 1; dp[1][2] = 0; diff --git a/codes/typescript/chapter_dynamic_programming/knapsack.ts b/codes/typescript/chapter_dynamic_programming/knapsack.ts index 39ced7f3..7fcef31e 100644 --- a/codes/typescript/chapter_dynamic_programming/knapsack.ts +++ b/codes/typescript/chapter_dynamic_programming/knapsack.ts @@ -21,8 +21,7 @@ function knapsackDFS( } // 计算不放入和放入物品 i 的最大价值 const no = knapsackDFS(wgt, val, i - 1, c); - const yes = - knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]; + const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]; // 返回两种方案中价值更大的那一个 return Math.max(no, yes); } diff --git a/codes/typescript/chapter_greedy/coin_change_greedy.ts b/codes/typescript/chapter_greedy/coin_change_greedy.ts index 678aaa21..303ef7cc 100644 --- a/codes/typescript/chapter_greedy/coin_change_greedy.ts +++ b/codes/typescript/chapter_greedy/coin_change_greedy.ts @@ -5,7 +5,7 @@ */ /* 零钱兑换:贪心 */ -function coin_change_greedy(coins: number[], amt: number): number { +function coinChangeGreedy(coins: number[], amt: number): number { // 假设 coins 数组有序 let i = coins.length - 1; let count = 0; @@ -27,24 +27,24 @@ function coin_change_greedy(coins: number[], amt: number): number { // 贪心:能够保证找到全局最优解 let coins: number[] = [1, 5, 10, 20, 50, 100]; let amt: number = 186; -let res: number = coin_change_greedy(coins, amt); +let res: number = coinChangeGreedy(coins, amt); console.log(`\ncoins = ${coins}, amt = ${amt}`); console.log(`凑到 ${amt} 所需的最少硬币数量为 ${res}`); // 贪心:无法保证找到全局最优解 coins = [1, 20, 50]; amt = 60; -res = coin_change_greedy(coins, amt); +res = coinChangeGreedy(coins, amt); console.log(`\ncoins = ${coins}, amt = ${amt}`); console.log(`凑到 ${amt} 所需的最少硬币数量为 ${res}`); -console.log("实际上需要的最少数量为 3 ,即 20 + 20 + 20"); +console.log('实际上需要的最少数量为 3 ,即 20 + 20 + 20'); // 贪心:无法保证找到全局最优解 coins = [1, 49, 50]; amt = 98; -res = coin_change_greedy(coins, amt); +res = coinChangeGreedy(coins, amt); console.log(`\ncoins = ${coins}, amt = ${amt}`); console.log(`凑到 ${amt} 所需的最少硬币数量为 ${res}`); -console.log("实际上需要的最少数量为 2 ,即 49 + 49"); +console.log('实际上需要的最少数量为 2 ,即 49 + 49'); -export { }; +export {}; diff --git a/codes/typescript/chapter_greedy/fractional_knapsack.ts b/codes/typescript/chapter_greedy/fractional_knapsack.ts index dfcf26ae..55d6b455 100644 --- a/codes/typescript/chapter_greedy/fractional_knapsack.ts +++ b/codes/typescript/chapter_greedy/fractional_knapsack.ts @@ -16,11 +16,11 @@ class Item { } /* 分数背包:贪心 */ -function fractional_knapsack(wgt: number[], val: number[], cap: number): number { +function fractionalKnapsack(wgt: number[], val: number[], cap: number): number { // 创建物品列表,包含两个属性:重量、价值 const items: Item[] = wgt.map((w, i) => new Item(w, val[i])); // 按照单位价值 item.v / item.w 从高到低进行排序 - items.sort((a, b) => (b.v / b.w) - (a.v / a.w)); + items.sort((a, b) => b.v / b.w - a.v / a.w); // 循环贪心选择 let res = 0; for (const item of items) { @@ -44,7 +44,7 @@ const val: number[] = [50, 120, 150, 210, 240]; const cap: number = 50; // 贪心算法 -const res: number = fractional_knapsack(wgt, val, cap); +const res: number = fractionalKnapsack(wgt, val, cap); console.log(`不超过背包容量的最大物品价值为 ${res}`); -export { }; +export {}; diff --git a/codes/typescript/chapter_greedy/max_capacity.ts b/codes/typescript/chapter_greedy/max_capacity.ts index 2f8d99be..3bc62b13 100644 --- a/codes/typescript/chapter_greedy/max_capacity.ts +++ b/codes/typescript/chapter_greedy/max_capacity.ts @@ -5,9 +5,10 @@ */ /* 最大容量:贪心 */ -function max_capacity(ht: number[]): number { +function maxCapacity(ht: number[]): number { // 初始化 i, j 分列数组两端 - let i = 0, j = ht.length - 1; + let i = 0, + j = ht.length - 1; // 初始最大容量为 0 let res = 0; // 循环贪心选择,直至两板相遇 @@ -29,7 +30,7 @@ function max_capacity(ht: number[]): number { const ht: number[] = [3, 8, 5, 2, 7, 7, 3, 4]; // 贪心算法 -const res: number = max_capacity(ht); +const res: number = maxCapacity(ht); console.log(`最大容量为 ${res}`); -export { }; +export {}; diff --git a/codes/typescript/chapter_greedy/max_product_cutting.ts b/codes/typescript/chapter_greedy/max_product_cutting.ts index 2d21c531..8eb0bff5 100644 --- a/codes/typescript/chapter_greedy/max_product_cutting.ts +++ b/codes/typescript/chapter_greedy/max_product_cutting.ts @@ -5,7 +5,7 @@ */ /* 最大切分乘积:贪心 */ -function max_product_cutting(n: number): number { +function maxProductCutting(n: number): number { // 当 n <= 3 时,必须切分出一个 1 if (n <= 3) { return 1 * (n - 1); @@ -29,7 +29,7 @@ function max_product_cutting(n: number): number { let n: number = 58; // 贪心算法 -let res: number = max_product_cutting(n); +let res: number = maxProductCutting(n); console.log(`最大切分乘积为 ${res}`); -export { }; +export {}; diff --git a/codes/typescript/chapter_heap/top_k.ts b/codes/typescript/chapter_heap/top_k.ts index 09e2daae..287997a0 100644 --- a/codes/typescript/chapter_heap/top_k.ts +++ b/codes/typescript/chapter_heap/top_k.ts @@ -9,7 +9,7 @@ import { MaxHeap } from './my_heap'; /* 基于堆查找数组中最大的 k 个元素 */ function top_k_heap(nums: number[], k: number): number[] { // 将堆中所有元素取反,从而用大顶堆来模拟小顶堆 - const invertedNums = nums.map(num => -num); + const invertedNums = nums.map((num) => -num); // 将数组的前 k 个元素入堆 const heap = new MaxHeap(invertedNums.slice(0, k)); // 从第 k+1 个元素开始,保持堆的长度为 k @@ -23,7 +23,7 @@ function top_k_heap(nums: number[], k: number): number[] { // 取出堆中元素 const maxHeap = heap.getMaxHeap(); // 对堆中元素取相反数 - const invertedMaxHeap = maxHeap.map(num => -num); + const invertedMaxHeap = maxHeap.map((num) => -num); return invertedMaxHeap; } diff --git a/codes/typescript/chapter_searching/binary_search_edge.ts b/codes/typescript/chapter_searching/binary_search_edge.ts index 704ed823..08e91f07 100644 --- a/codes/typescript/chapter_searching/binary_search_edge.ts +++ b/codes/typescript/chapter_searching/binary_search_edge.ts @@ -5,7 +5,6 @@ */ import { binarySearchInsertion } from './binary_search_insertion'; - /* 二分查找最左一个 target */ function binarySearchLeftEdge(nums: Array, target: number): number { // 等价于查找 target 的插入点 @@ -34,7 +33,7 @@ function binarySearchRightEdge(nums: Array, target: number): number { /* Driver Code */ // 包含重复元素的数组 -let nums= [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; +let nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]; console.log('\n数组 nums = ' + nums); // 二分查找左边界和右边界 for (const target of [6, 7]) { diff --git a/codes/typescript/chapter_searching/binary_search_insertion.ts b/codes/typescript/chapter_searching/binary_search_insertion.ts index 19a1aea5..bb470e28 100644 --- a/codes/typescript/chapter_searching/binary_search_insertion.ts +++ b/codes/typescript/chapter_searching/binary_search_insertion.ts @@ -5,7 +5,10 @@ */ /* 二分查找插入点(无重复元素) */ -function binarySearchInsertionSimple(nums: Array, target: number): number { +function binarySearchInsertionSimple( + nums: Array, + target: number +): number { let i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1] while (i <= j) { diff --git a/codes/typescript/chapter_sorting/bubble_sort.ts b/codes/typescript/chapter_sorting/bubble_sort.ts index f0a7afc3..c07ab790 100644 --- a/codes/typescript/chapter_sorting/bubble_sort.ts +++ b/codes/typescript/chapter_sorting/bubble_sort.ts @@ -8,7 +8,7 @@ function bubbleSort(nums: number[]): void { // 外循环:未排序区间为 [0, i] for (let i = nums.length - 1; i > 0; i--) { - // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (let j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] @@ -25,7 +25,7 @@ function bubbleSortWithFlag(nums: number[]): void { // 外循环:未排序区间为 [0, i] for (let i = nums.length - 1; i > 0; i--) { let flag = false; // 初始化标志位 - // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (let j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/typescript/chapter_tree/binary_search_tree.ts b/codes/typescript/chapter_tree/binary_search_tree.ts index 248c7b5a..c5608aed 100644 --- a/codes/typescript/chapter_tree/binary_search_tree.ts +++ b/codes/typescript/chapter_tree/binary_search_tree.ts @@ -84,7 +84,8 @@ class BinarySearchTree { // 子节点数量 = 0 or 1 if (cur.left === null || cur.right === null) { // 当子节点数量 = 0 / 1 时, child = null / 该子节点 - const child: TreeNode | null = cur.left !== null ? cur.left : cur.right; + const child: TreeNode | null = + cur.left !== null ? cur.left : cur.right; // 删除节点 cur if (cur !== this.root) { if (pre!.left === cur) pre!.left = child; @@ -122,7 +123,9 @@ printTree(bst.getRoot()); /* 查找节点 */ const node = bst.search(7); -console.log('\n查找到的节点对象为 ' + node + ',节点值 = ' + (node ? node.val : 'null')); +console.log( + '\n查找到的节点对象为 ' + node + ',节点值 = ' + (node ? node.val : 'null') +); /* 插入节点 */ bst.insert(16); diff --git a/docs/chapter_backtracking/permutations_problem.md b/docs/chapter_backtracking/permutations_problem.md index 9e88a373..06a0453c 100644 --- a/docs/chapter_backtracking/permutations_problem.md +++ b/docs/chapter_backtracking/permutations_problem.md @@ -263,7 +263,7 @@ [class]{}-[func]{permutations_ii} ``` -假设元素两两之间互不相同,则 $n$ 个元素共有 $n!$ 种排列(阶乘);在记录结果时,需要复制长度为 $n$ 的列表,使用 $O(n)$ 时间。因此,**时间复杂度为 $O(n!n)$** 。 +假设元素两两之间互不相同,则 $n$ 个元素共有 $n!$ 种排列(阶乘);在记录结果时,需要复制长度为 $n$ 的列表,使用 $O(n)$ 时间。**因此时间复杂度为 $O(n!n)$** 。 最大递归深度为 $n$ ,使用 $O(n)$ 栈帧空间。`selected` 使用 $O(n)$ 空间。同一时刻最多共有 $n$ 个 `duplicated` ,使用 $O(n^2)$ 空间。**因此空间复杂度为 $O(n^2)$** 。 diff --git a/docs/chapter_hashing/hash_algorithm.md b/docs/chapter_hashing/hash_algorithm.md index 010d4527..16ec1790 100644 --- a/docs/chapter_hashing/hash_algorithm.md +++ b/docs/chapter_hashing/hash_algorithm.md @@ -340,13 +340,13 @@ $$ === "JS" ```javascript title="built_in_hash.js" - + // JavaScript 未提供内置 hash code 函数 ``` === "TS" ```typescript title="built_in_hash.ts" - + // TypeScript 未提供内置 hash code 函数 ``` === "C" diff --git a/docs/chapter_tree/binary_search_tree.md b/docs/chapter_tree/binary_search_tree.md index 6351d571..af134c00 100755 --- a/docs/chapter_tree/binary_search_tree.md +++ b/docs/chapter_tree/binary_search_tree.md @@ -66,7 +66,7 @@ === "TS" ```typescript title="binary_search_tree.ts" - [class]{}-[func]{search} + [class]{BinarySearchTree}-[func]{search} ``` === "C" @@ -152,7 +152,7 @@ === "TS" ```typescript title="binary_search_tree.ts" - [class]{}-[func]{insert} + [class]{BinarySearchTree}-[func]{insert} ``` === "C" @@ -263,7 +263,7 @@ === "TS" ```typescript title="binary_search_tree.ts" - [class]{}-[func]{remove} + [class]{BinarySearchTree}-[func]{remove} ``` === "C"