mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 13:56:28 +08:00
prepare 1.0.0b5 release
This commit is contained in:
parent
3530f8c5ce
commit
2b54352bec
2 changed files with 4 additions and 4 deletions
|
@ -7,7 +7,7 @@
|
||||||
const { MaxHeap } = require('./my_heap');
|
const { MaxHeap } = require('./my_heap');
|
||||||
|
|
||||||
/* 基于堆查找数组中最大的 k 个元素 */
|
/* 基于堆查找数组中最大的 k 个元素 */
|
||||||
function top_k_heap(nums, k) {
|
function topKHeap(nums, k) {
|
||||||
// 使用大顶堆 MaxHeap,对数组 nums 取相反数
|
// 使用大顶堆 MaxHeap,对数组 nums 取相反数
|
||||||
const invertedNums = nums.map((num) => -num);
|
const invertedNums = nums.map((num) => -num);
|
||||||
// 将数组的前 k 个元素入堆
|
// 将数组的前 k 个元素入堆
|
||||||
|
@ -30,5 +30,5 @@ function top_k_heap(nums, k) {
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
const nums = [1, 7, 6, 3, 2];
|
const nums = [1, 7, 6, 3, 2];
|
||||||
const k = 3;
|
const k = 3;
|
||||||
const res = top_k_heap(nums, k);
|
const res = topKHeap(nums, k);
|
||||||
console.log(`最大的 ${k} 个元素为`, res);
|
console.log(`最大的 ${k} 个元素为`, res);
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
import { MaxHeap } from './my_heap';
|
import { MaxHeap } from './my_heap';
|
||||||
|
|
||||||
/* 基于堆查找数组中最大的 k 个元素 */
|
/* 基于堆查找数组中最大的 k 个元素 */
|
||||||
function top_k_heap(nums: number[], k: number): number[] {
|
function topKHeap(nums: number[], k: number): number[] {
|
||||||
// 将堆中所有元素取反,从而用大顶堆来模拟小顶堆
|
// 将堆中所有元素取反,从而用大顶堆来模拟小顶堆
|
||||||
const invertedNums = nums.map((num) => -num);
|
const invertedNums = nums.map((num) => -num);
|
||||||
// 将数组的前 k 个元素入堆
|
// 将数组的前 k 个元素入堆
|
||||||
|
@ -30,5 +30,5 @@ function top_k_heap(nums: number[], k: number): number[] {
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
const nums = [1, 7, 6, 3, 2];
|
const nums = [1, 7, 6, 3, 2];
|
||||||
const k = 3;
|
const k = 3;
|
||||||
const res = top_k_heap(nums, k);
|
const res = topKHeap(nums, k);
|
||||||
console.log(`最大的 ${k} 个元素为`, res);
|
console.log(`最大的 ${k} 个元素为`, res);
|
||||||
|
|
Loading…
Reference in a new issue