设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。

  • push(x) -- 将元素 x 推入栈中。
  • pop() -- 删除栈顶的元素。
  • top() -- 获取栈顶元素。
  • getMin() -- 检索栈中的最小元素。

示例:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> 返回 -3.
minStack.pop();
minStack.top(); --> 返回 0.
minStack.getMin(); --> 返回 -2.
class MinStack:

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.data = []
             def push(self, x):
        """
        :type x: int
        :rtype: void
        """
        if len(self.data) == 0:
            return self.data.append((x,x))
        else:
            min_num=self.data[-1][1]
            return  self.data.append((x,x)) if min_num > x else self.data.append((x,min_num))
        
             def pop(self):
        """
        :rtype: void
        """
        return self.data.pop()     def top(self):
        """
        :rtype: int
        """
        return self.data[-1][0]
             def getMin(self):
        """
        :rtype: int
        """
        return self.data[-1][1] # Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()

最新文章

  1. 【社工】NodeJS 应用仓库钓鱼
  2. [emacs] 使用ggtags浏览代码
  3. 事件委托和JQ事件绑定总结
  4. 实战-Fluxion与wifi热点伪造、钓鱼、中间人攻击、wifi破解
  5. ubuntu wubi安装注意事项
  6. hdu 2203亲和串 (kmp)
  7. 学习linux的一些指令
  8. HP 3par多路径安装方法
  9. 将传统 WPF 程序迁移到 DotNetCore 3.0
  10. python 协程、I/O模型
  11. 解决在jupyter notebook中遇到的ImportError: matplotlib is required for plotting问题
  12. Sprint 冲刺第三阶段第一天
  13. linux下find命令详解
  14. 误删文件不用怕 grep命令帮你恢复
  15. 对dump的文件进行状态统计
  16. hdu1171 Big Event in HDU(01背包) 2016-05-28 16:32 75人阅读 评论(0) 收藏
  17. 【mysql】Innodb三大特性之adaptive hash index
  18. postman设置环境变量,字段值经过json转换后数值字节长度超过上限的问题
  19. 洛谷 P2123 皇后游戏 解题报告
  20. zabbix服务器搭建

热门文章

  1. 性能测试工具Loadrunner使用经验小结(原创更新版)
  2. maven将依赖打入jar包
  3. Struts2拦截器配置和使用
  4. egret 开发总结
  5. PHP中的构造方法和析构方法
  6. Vue2.0 render:h => h(App)
  7. app耗电量测试工具--PowerTutor
  8. 【Java并发编程】之二:线程中断
  9. ACID和CAP, BASE
  10. 2018九省联考(SHOI2018)