File tree Expand file tree Collapse file tree 1 file changed +7
-1
lines changed Expand file tree Collapse file tree 1 file changed +7
-1
lines changed Original file line number Diff line number Diff 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
@@ -416,7 +421,7 @@ class Heap {
416421 let left = 1 ; // left 是左子节点下标 left + 1 则是右子节点下标
417422 let searchChild = this .compare (left, left + 1 ) > 0 ? left + 1 : left;
418423
419- while (searchChild !== undefined && this .compare (index, searchChild) > 0 ) { // 注意compare参数顺序
424+ while (this .compare (index, searchChild) > 0 ) { // 注意compare参数顺序
420425 [this .queue [index], this .queue [searchChild]] = [this .queue [searchChild], this .queue [index]];
421426
422427 // 更新下标
@@ -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+
You can’t perform that action at this time.
0 commit comments