Format JS and TS code.

This commit is contained in:
krahets 2023-09-02 23:07:47 +08:00
parent f96f583771
commit 978857570f
35 changed files with 75 additions and 74 deletions

View File

@ -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]

View File

@ -32,4 +32,3 @@ function climbingStairsBacktrack(n) {
const n = 9;
const res = climbingStairsBacktrack(n);
console.log(`${n} 阶楼梯共有 ${res} 种方案`);

View File

@ -38,4 +38,3 @@ let res = climbingStairsDP(n);
console.log(`${n} 阶楼梯共有 ${res} 种方案`);
res = climbingStairsDPComp(n);
console.log(`${n} 阶楼梯共有 ${res} 种方案`);

View File

@ -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];

View File

@ -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}`);
console.log(`从左上角到右下角的最小路径和为 ${res}`);

View File

@ -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');

View File

@ -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}`);

View File

@ -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}`);

View File

@ -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}`);

View File

@ -151,7 +151,6 @@ console.log(`\n堆元素数量为 ${size}`);
let isEmpty = maxHeap.isEmpty();
console.log(`\n堆是否为空 ${isEmpty}`);
module.exports = {
MaxHeap,
};

View File

@ -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;
}

View File

@ -60,5 +60,5 @@ for (const target of [2, 6, 20]) {
}
module.exports = {
binarySearchInsertion
};
binarySearchInsertion,
};

View File

@ -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;
}

View File

@ -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]

View File

@ -5,7 +5,7 @@
*/
/* for 循环 */
/* for 循环 */
fn for_loop(n: i32) -> i32 {
let mut res = 0;
// 循环求和 1, 2, ..., n-1, n

View File

@ -98,4 +98,4 @@ traverse(nums);
let index = find(nums, 3);
console.log('在 nums 中查找元素 3 ,得到索引 =', index);
export { };
export {};

View File

@ -62,4 +62,4 @@ res.forEach((state) => {
state.forEach((row) => console.log(row));
});
export {};
export {};

View File

@ -69,4 +69,4 @@ console.log(`while 循环(两次更新)求和结果 res = ${res}`);
const resStr = nestedForLoop(n);
console.log(`双层 for 循环的遍历结果 ${resStr}`);
export {};
export {};

View File

@ -45,4 +45,4 @@ console.log(`尾递归函数的求和结果 res = ${res}`);
res = fib(n);
console.log(`斐波那契数列的第 ${n} 项为 ${res}`);
export {};
export {};

View File

@ -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]

View File

@ -38,4 +38,4 @@ const n = 9;
const res = climbingStairsBacktrack(n);
console.log(`${n} 阶楼梯共有 ${res} 种方案`);
export { };
export {};

View File

@ -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;

View File

@ -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);
}

View File

@ -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 {};

View File

@ -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 {};

View File

@ -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 {};

View File

@ -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 {};

View File

@ -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;
}

View File

@ -5,7 +5,6 @@
*/
import { binarySearchInsertion } from './binary_search_insertion';
/* 二分查找最左一个 target */
function binarySearchLeftEdge(nums: Array<number>, target: number): number {
// 等价于查找 target 的插入点
@ -34,7 +33,7 @@ function binarySearchRightEdge(nums: Array<number>, 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]) {

View File

@ -5,7 +5,10 @@
*/
/* 二分查找插入点(无重复元素) */
function binarySearchInsertionSimple(nums: Array<number>, target: number): number {
function binarySearchInsertionSimple(
nums: Array<number>,
target: number
): number {
let i = 0,
j = nums.length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {

View File

@ -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]

View File

@ -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);

View File

@ -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)$** 。

View File

@ -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"

View File

@ -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"