Movatterモバイル変換


[0]ホーム

URL:


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

155. 最小栈 #23

Open
Open
Labels
@Geekhyt

Description

@Geekhyt

原题链接

辅助栈

  1. stack 支持常规的 push、pop、top 操作。
  2. 定义一个辅助栈 resStack ,将最小值一直保持在栈顶,来支持常数时间复杂度获取。
constMinStack=function(){this.stack=[];this.resStack=[Infinity];};MinStack.prototype.push=function(x){this.stack.push(x);this.resStack.push(Math.min(this.resStack[this.resStack.length-1],x));};MinStack.prototype.pop=function(){this.stack.pop();this.resStack.pop();};MinStack.prototype.top=function(){returnthis.stack[this.stack.length-1];};MinStack.prototype.getMin=function(){returnthis.resStack[this.resStack.length-1];};
  • 时间复杂度: O(1)
  • 空间复杂度: O(n)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp