leetcode-easy-design-155 Min Stack
2024-09-02 14:03:15
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()
最新文章
- Strategy pattern策略模式
- 阿里云服务器被挖矿minerd入侵的解决办法
- Centos6.5和Centos7 php环境搭建如何实现呢
- as3中的多线程
- SQL日语词汇
- 第29题:推断一个序列是否是还有一个push序列的pop序列
- Cocos2d-x中SQLite数据库管理工具
- C# window service的创建
- Activiti工作流学习-----基于5.19.0版本(7)
- Mysql User表为空
- ASP.NET,Razor语句中@符号的意义
- form不提交问题
- L6,Percy Buttons
- SpringMvc笔记-注解
- Docker(六):Docker 三剑客之 Docker Swarm
- win7待机时间设置,睡眠时间设置
- Jetson TX2(3)opencv3 打开usb摄像头
- redhat6.5 redis单节点多实例3A集群搭建
- Git使用详细教程(7):.gitignore使用详解
- [nodejs]er_bad_field_error NaN in where clause