修复了 JavaScript 解法二,堆 pop 方法无法处理数组长度 <= 1 的边界情况的问题
This commit is contained in:
parent
74f1810da6
commit
1c390b3562
|
|
@ -405,6 +405,11 @@ class Heap {
|
||||||
|
|
||||||
// 获取堆顶元素并移除
|
// 获取堆顶元素并移除
|
||||||
pop() {
|
pop() {
|
||||||
|
// 边界情况,只有一个元素或没有元素应直接弹出
|
||||||
|
if (this.size() <= 1) {
|
||||||
|
return this.queue.pop()
|
||||||
|
}
|
||||||
|
|
||||||
// 堆顶元素
|
// 堆顶元素
|
||||||
const out = this.queue[0];
|
const out = this.queue[0];
|
||||||
|
|
||||||
|
|
@ -608,3 +613,4 @@ impl Solution {
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue