修复了 JavaScript 解法二,堆 pop 方法无法处理数组长度 <= 1 的边界情况的问题

This commit is contained in:
xqsrpanz
2024-04-26 18:01:03 +08:00
parent 74f1810da6
commit 1c390b3562

View File

@ -405,6 +405,11 @@ class Heap {
// 获取堆顶元素并移除
pop() {
// 边界情况,只有一个元素或没有元素应直接弹出
if (this.size() <= 1) {
return this.queue.pop()
}
// 堆顶元素
const out = this.queue[0];
@ -608,3 +613,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>