mycode   21.48%

class MinStack(object):

    def __init__(self):
"""
initialize your data structure here.
"""
self.res = [] def push(self, x):
"""
:type x: int
:rtype: None
"""
self.res.append(x) def pop(self):
"""
:rtype: None
"""
if not self.res:
return None
else:
val = self.res[-1]
self.res[:] = self.res[:-1]
return val def top(self):
"""
:rtype: int
"""
if not self.res:
return None
else:
return self.res[-1] def getMin(self):
"""
:rtype: int
"""
if not self.res:
return None
else:
return min(self.res) # 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()

列表是有pop操作的

self.res.pop()

参考

始终更新最小值,所以节省 了时间

class MinStack(object):

    def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
self.minval = 99999999999999999 def push(self, x):
"""
:type x: int
:rtype: None
"""
if(x < self.minval):
self.minval = x
self.stack.append(x) def pop(self):
"""
:rtype: None
"""
if(self.stack[-1] == self.minval):
self.minval = 99999999999
for num in self.stack[0:-1]:
if(num < self.minval):
self.minval = num
self.stack.pop(-1) def top(self):
"""
:rtype: int
"""
return self.stack[-1] def getMin(self):
"""
:rtype: int
"""
return self.minval # 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. Strategy pattern策略模式
  2. 阿里云服务器被挖矿minerd入侵的解决办法
  3. Centos6.5和Centos7 php环境搭建如何实现呢
  4. as3中的多线程
  5. SQL日语词汇
  6. 第29题:推断一个序列是否是还有一个push序列的pop序列
  7. Cocos2d-x中SQLite数据库管理工具
  8. C# window service的创建
  9. Activiti工作流学习-----基于5.19.0版本(7)
  10. Mysql User表为空
  11. ASP.NET,Razor语句中@符号的意义
  12. form不提交问题
  13. L6,Percy Buttons
  14. SpringMvc笔记-注解
  15. Docker(六):Docker 三剑客之 Docker Swarm
  16. win7待机时间设置,睡眠时间设置
  17. Jetson TX2(3)opencv3 打开usb摄像头
  18. redhat6.5 redis单节点多实例3A集群搭建
  19. Git使用详细教程(7):.gitignore使用详解
  20. [nodejs]er_bad_field_error NaN in where clause

热门文章

  1. pycharm terminal打开在虚拟环境
  2. 第七篇 CSS盒子
  3. laravel 使用 intervention/image 的注意方法
  4. 依赖注入 php
  5. 2019.10.9wechat反弹shell复现
  6. verilog 实现DDS
  7. 牛客小白月赛12 J 月月查华华的手机 (序列自动机模板题)
  8. Transformer, ELMo, GPT, 到Bert
  9. php中限制ip段访问、禁止ip提交表单的代码
  10. js 中dindexof()用法