diff --git "a/problems/0347.345円211円215円K344円270円252円351円253円230円351円242円221円345円205円203円347円264円240円.md" "b/problems/0347.345円211円215円K344円270円252円351円253円230円351円242円221円345円205円203円347円264円240円.md" index 34d9f82c1b..cca9b0edce 100644 --- "a/problems/0347.345円211円215円K344円270円252円351円253円230円351円242円221円345円205円203円347円264円240円.md" +++ "b/problems/0347.345円211円215円K344円270円252円351円253円230円351円242円221円345円205円203円347円264円240円.md" @@ -405,6 +405,11 @@ class Heap { // 获取堆顶元素并移除 pop() { + // 边界情况,只有一个元素或没有元素应直接弹出 + if (this.size() <= 1) { + return this.queue.pop() + } + // 堆顶元素 const out = this.queue[0]; @@ -416,7 +421,7 @@ class Heap { let left = 1; // left 是左子节点下标 left + 1 则是右子节点下标 let searchChild = this.compare(left, left + 1)> 0 ? left + 1 : left; - while (searchChild !== undefined && this.compare(index, searchChild)> 0) { // 注意compare参数顺序 + while (this.compare(index, searchChild)> 0) { // 注意compare参数顺序 [this.queue[index], this.queue[searchChild]] = [this.queue[searchChild], this.queue[index]]; // 更新下标 @@ -608,3 +613,4 @@ impl Solution { +