ITPub博客

首页 > IT职业 > IT生活 > Lintcode12 Min Stack solution 题解

Lintcode12 Min Stack solution 题解

IT生活 作者:牛客001 时间:2017-07-31 22:44:28 0 删除 编辑

【题目描述】 Implement a stack with min() function, which will return the smallest number in the stack. It should support push, pop and min operation all in O(1) cost. Notice:min operation will never be called if there is no number in the stack. 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值。 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成。 注意:如果堆栈中没有数字则不能进行min方法的调用 【题目链接】
http://www.lintcode.com/en/problem/min-stack/
【题目解析】 利用两个栈结构,其中一个是主要的正常stack,满足pop(), push()的O(1)时间要求,另外一个作为辅助的minStack,仅存入min的integer。 min = Integer.parseInt(minStack.peek().toString()); push()时,如果number >= min,则push到minStack上 pop()时,如果number == min,也从minStack上pop 题中的例子,最终stack为[2, 3, 1], minStack为 [2, 1] 【答案链接】
http://www.jiuzhang.com/solutions/min-stack/

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31478388/viewspace-2142879/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2017-07-25

  • 博文量
    13
  • 访问量
    3948