Skip to content

Commit 1c390b3

Browse files
committed
修复了 JavaScript 解法二,堆 pop 方法无法处理数组长度 <= 1 的边界情况的问题
1 parent 74f1810 commit 1c390b3

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

problems/0347.前K个高频元素.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,11 @@ class Heap {
405405

406406
// 获取堆顶元素并移除
407407
pop() {
408+
// 边界情况,只有一个元素或没有元素应直接弹出
409+
if (this.size() <= 1) {
410+
return this.queue.pop()
411+
}
412+
408413
// 堆顶元素
409414
const out = this.queue[0];
410415

@@ -608,3 +613,4 @@ impl Solution {
608613
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
609614
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
610615
</a>
616+

0 commit comments

Comments
 (0)