Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c17953b

Browse files
Merge pull request youngyangyang04#2542 from xqsrpanz/master
Update 0347.前K个高频元素.md
2 parents 0a95ad0 + bcdf3e4 commit c17953b

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
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

@@ -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+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /