题目描述

定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。

# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.stack = []
self.min_stack = []
def push(self, node):
# write code here
self.stack.append(node)
if not self.min_stack or node <= self.min_stack[-1]:
self.min_stack.append(node)
else:
self.min_stack.append(self.min_stack[-1])
def pop(self):
# write code here
#if not self.stack:
self.stack.pop()
self.min_stack.pop()
def top(self):
# write code here
return self.stack[-1]
def min(self):
# write code here
return self.min_stack[-1]

最新文章

  1. 一起学 Java(三) 集合框架、数据结构、泛型
  2. IOS setValue forKey
  3. iOS 多渠道打包 编译脚本
  4. think in java 读书笔记 3 —— 数据报
  5. 在mac上搭建python环境
  6. 杭电 2034 人见人爱A-B
  7. A题笔记(13)
  8. 浅度围观SBJson
  9. 织梦DEDECMS {dede:field name=&#39;position&#39;/}标签增加其它属性的
  10. PHP无限级分类生成树实例代码
  11. Windows下Node.js开发环境搭建-合适的开发环境
  12. cp命令的实现
  13. [渣译文] SignalR 2.0 系列: SignalR简介
  14. Linux的文件夹配置
  15. 输入流IS和输出流OS学习总结
  16. Runtime &quot;Apache Tomcat v6.0 (3)&quot; is invalid. The JRE could not be found. Edit the server and change the JRE location解决方案
  17. 《剑指offer》 反转链表
  18. JS执行一次任务与定期任务与清除执行
  19. 【Little Demo】左右按钮tab选项卡双切换
  20. python lambda 函数

热门文章

  1. IOS swift实现密码的显示与隐藏切换
  2. Spring ThreadPoolTaskExecutor队列满的异常处理
  3. Ehab and subtraction(思维题)
  4. ReflectionUtil
  5. 使用spring boot admin
  6. 使用open live writer客户端写博客(亲测有效)
  7. json数据前台解析 修改check属性用prop()
  8. Python 为threading.Thread添加 terminate
  9. JS语法字典---网友总结
  10. asp.net MVC 4.0 Controller回顾——ModelBinding实现过程