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]