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