mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-08 13:24:07 +08:00
feat: add top_k.c and refactor top_k.js (#889)
* Add top_k.c based on my_heap.c * Improve the implementation of top_k.js * Add a comment to top_k
This commit is contained in:
@@ -12,6 +12,7 @@ import java.util.*;
|
||||
public class top_k {
|
||||
/* 基于堆查找数组中最大的 k 个元素 */
|
||||
static Queue<Integer> topKHeap(int[] nums, int k) {
|
||||
// 初始化小顶堆
|
||||
Queue<Integer> heap = new PriorityQueue<Integer>();
|
||||
// 将数组的前 k 个元素入堆
|
||||
for (int i = 0; i < k; i++) {
|
||||
|
||||
Reference in New Issue
Block a user