diff --git a/codes/javascript/chapter_array_and_linkedlist/array.js b/codes/javascript/chapter_array_and_linkedlist/array.js index 8781d312d..95bfe283a 100644 --- a/codes/javascript/chapter_array_and_linkedlist/array.js +++ b/codes/javascript/chapter_array_and_linkedlist/array.js @@ -53,7 +53,7 @@ function traverse(nums) { count++; } // 直接遍历数组 - for (let num of nums) { + for (const num of nums) { count += 1; } } diff --git a/codes/javascript/chapter_dynamic_programming/climbing_stairs_backtrack.js b/codes/javascript/chapter_dynamic_programming/climbing_stairs_backtrack.js index d8703e31f..96c828604 100644 --- a/codes/javascript/chapter_dynamic_programming/climbing_stairs_backtrack.js +++ b/codes/javascript/chapter_dynamic_programming/climbing_stairs_backtrack.js @@ -9,7 +9,7 @@ function backtrack(choices, state, n, res) { // 当爬到第 n 阶时,方案数量加 1 if (state === n) res.set(0, res.get(0) + 1); // 遍历所有选择 - for (choice of choices) { + for (const choice of choices) { // 剪枝:不允许越过第 n 阶 if (state + choice > n) break; // 尝试:做出选择,更新状态 diff --git a/codes/javascript/chapter_graph/graph_adjacency_list.js b/codes/javascript/chapter_graph/graph_adjacency_list.js index dfd46303d..bc4a948bf 100644 --- a/codes/javascript/chapter_graph/graph_adjacency_list.js +++ b/codes/javascript/chapter_graph/graph_adjacency_list.js @@ -70,7 +70,7 @@ class GraphAdjList { // 在邻接表中删除顶点 vet 对应的链表 this.adjList.delete(vet); // 遍历其他顶点的链表,删除所有包含 vet 的边 - for (let set of this.adjList.values()) { + for (const set of this.adjList.values()) { const index = set.indexOf(vet); if (index > -1) { set.splice(index, 1); diff --git a/codes/javascript/chapter_hashing/hash_map_chaining.js b/codes/javascript/chapter_hashing/hash_map_chaining.js index 7d4bd79c9..151cb5a57 100644 --- a/codes/javascript/chapter_hashing/hash_map_chaining.js +++ b/codes/javascript/chapter_hashing/hash_map_chaining.js @@ -82,7 +82,7 @@ class HashMapChaining { for (let i = 0; i < bucket.length; i++) { if (bucket[i].key === key) { bucket.splice(i, 1); - this.size--; + this.#size--; break; } } diff --git a/codes/javascript/chapter_heap/my_heap.js b/codes/javascript/chapter_heap/my_heap.js index 52e521383..d37b64a10 100644 --- a/codes/javascript/chapter_heap/my_heap.js +++ b/codes/javascript/chapter_heap/my_heap.js @@ -115,6 +115,11 @@ class MaxHeap { print() { printHeap(this.#maxHeap); } + + /* 取出堆中元素 */ + getMaxHeap() { + return this.#maxHeap; + } } /* Driver Code */ @@ -145,3 +150,8 @@ 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 new file mode 100644 index 000000000..8d8ced429 --- /dev/null +++ b/codes/javascript/chapter_heap/top_k.js @@ -0,0 +1,34 @@ +/** + * File: top_k.js + * Created Time: 2023-08-13 + * Author: Justin (xiefahit@gmail.com) + */ + +const { MaxHeap } = require('./my_heap'); + +/* 基于堆查找数组中最大的 k 个元素 */ +function top_k_heap(nums, k) { + // 使用大顶堆 MaxHeap,对数组 nums 取相反数 + const invertedNums = nums.map(num => -num); + // 将数组的前 k 个元素入堆 + const heap = new MaxHeap(invertedNums.slice(0, k)); + // 从第 k+1 个元素开始,保持堆的长度为 k + for (let i = k; i < invertedNums.length; i++) { + // 若当前元素小于堆顶元素,则将堆顶元素出堆、当前元素入堆 + if (invertedNums[i] < heap.peek()) { + heap.pop(); + heap.push(invertedNums[i]); + } + } + // 取出堆中元素 + const maxHeap = heap.getMaxHeap(); + // 对堆中元素取相反数 + const invertedMaxHeap = maxHeap.map(num => -num); + return invertedMaxHeap; +} + +/* Driver Code */ +const nums = [1, 7, 6, 3, 2]; +const k = 3; +const res = top_k_heap(nums, k); +console.log(`最大的 ${k} 个元素为`, res); diff --git a/codes/typescript/chapter_array_and_linkedlist/array.ts b/codes/typescript/chapter_array_and_linkedlist/array.ts index baaac39f8..e43d09906 100644 --- a/codes/typescript/chapter_array_and_linkedlist/array.ts +++ b/codes/typescript/chapter_array_and_linkedlist/array.ts @@ -53,7 +53,7 @@ function traverse(nums: number[]): void { count++; } // 直接遍历数组 - for (let num of nums) { + for (const num of nums) { count += 1; } } @@ -98,4 +98,4 @@ traverse(nums); let index = find(nums, 3); console.log('在 nums 中查找元素 3 ,得到索引 =', index); -export {}; +export { }; diff --git a/codes/typescript/chapter_dynamic_programming/climbing_stairs_backtrack.ts b/codes/typescript/chapter_dynamic_programming/climbing_stairs_backtrack.ts index 1bb5734a6..323680637 100644 --- a/codes/typescript/chapter_dynamic_programming/climbing_stairs_backtrack.ts +++ b/codes/typescript/chapter_dynamic_programming/climbing_stairs_backtrack.ts @@ -14,7 +14,7 @@ function backtrack( // 当爬到第 n 阶时,方案数量加 1 if (state === n) res.set(0, res.get(0) + 1); // 遍历所有选择 - for (let choice of choices) { + for (const choice of choices) { // 剪枝:不允许越过第 n 阶 if (state + choice > n) break; // 尝试:做出选择,更新状态 @@ -38,4 +38,4 @@ const n = 9; const res = climbingStairsBacktrack(n); console.log(`爬 ${n} 阶楼梯共有 ${res} 种方案`); -export {}; +export { }; diff --git a/codes/typescript/chapter_graph/graph_adjacency_list.ts b/codes/typescript/chapter_graph/graph_adjacency_list.ts index 0574bfc19..de0d9be42 100644 --- a/codes/typescript/chapter_graph/graph_adjacency_list.ts +++ b/codes/typescript/chapter_graph/graph_adjacency_list.ts @@ -70,7 +70,7 @@ class GraphAdjList { // 在邻接表中删除顶点 vet 对应的链表 this.adjList.delete(vet); // 遍历其他顶点的链表,删除所有包含 vet 的边 - for (let set of this.adjList.values()) { + for (const set of this.adjList.values()) { const index: number = set.indexOf(vet); if (index > -1) { set.splice(index, 1); diff --git a/codes/typescript/chapter_heap/my_heap.ts b/codes/typescript/chapter_heap/my_heap.ts index ed2e9b209..298fe56c9 100644 --- a/codes/typescript/chapter_heap/my_heap.ts +++ b/codes/typescript/chapter_heap/my_heap.ts @@ -114,6 +114,11 @@ class MaxHeap { public print(): void { printHeap(this.maxHeap); } + + /* 取出堆中元素 */ + public getMaxHeap(): number[] { + return this.maxHeap; + } } /* Driver Code */ @@ -144,3 +149,5 @@ console.log(`\n堆元素数量为 ${size}`); /* 判断堆是否为空 */ const isEmpty = maxHeap.isEmpty(); console.log(`\n堆是否为空 ${isEmpty}`); + +export { MaxHeap }; diff --git a/codes/typescript/chapter_heap/top_k.ts b/codes/typescript/chapter_heap/top_k.ts new file mode 100644 index 000000000..09e2daae0 --- /dev/null +++ b/codes/typescript/chapter_heap/top_k.ts @@ -0,0 +1,34 @@ +/** + * File: top_k.ts + * Created Time: 2023-08-13 + * Author: Justin (xiefahit@gmail.com) + */ + +import { MaxHeap } from './my_heap'; + +/* 基于堆查找数组中最大的 k 个元素 */ +function top_k_heap(nums: number[], k: number): number[] { + // 将堆中所有元素取反,从而用大顶堆来模拟小顶堆 + const invertedNums = nums.map(num => -num); + // 将数组的前 k 个元素入堆 + const heap = new MaxHeap(invertedNums.slice(0, k)); + // 从第 k+1 个元素开始,保持堆的长度为 k + for (let i = k; i < invertedNums.length; i++) { + // 若当前元素小于堆顶元素,则将堆顶元素出堆、当前元素入堆 + if (invertedNums[i] < heap.peek()) { + heap.pop(); + heap.push(invertedNums[i]); + } + } + // 取出堆中元素 + const maxHeap = heap.getMaxHeap(); + // 对堆中元素取相反数 + const invertedMaxHeap = maxHeap.map(num => -num); + return invertedMaxHeap; +} + +/* Driver Code */ +const nums = [1, 7, 6, 3, 2]; +const k = 3; +const res = top_k_heap(nums, k); +console.log(`最大的 ${k} 个元素为`, res);