python剑指offer 包含min函数的栈
2024-09-30 07:35:57
题目描述
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的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]
最新文章
- 一起学 Java(三) 集合框架、数据结构、泛型
- IOS setValue forKey
- iOS 多渠道打包 编译脚本
- think in java 读书笔记 3 —— 数据报
- 在mac上搭建python环境
- 杭电 2034 人见人爱A-B
- A题笔记(13)
- 浅度围观SBJson
- 织梦DEDECMS {dede:field name=&#39;position&#39;/}标签增加其它属性的
- PHP无限级分类生成树实例代码
- Windows下Node.js开发环境搭建-合适的开发环境
- cp命令的实现
- [渣译文] SignalR 2.0 系列: SignalR简介
- Linux的文件夹配置
- 输入流IS和输出流OS学习总结
- Runtime ";Apache Tomcat v6.0 (3)"; is invalid. The JRE could not be found. Edit the server and change the JRE location解决方案
- 《剑指offer》 反转链表
- JS执行一次任务与定期任务与清除执行
- 【Little Demo】左右按钮tab选项卡双切换
- python lambda 函数
热门文章
- IOS swift实现密码的显示与隐藏切换
- Spring ThreadPoolTaskExecutor队列满的异常处理
- Ehab and subtraction(思维题)
- ReflectionUtil
- 使用spring boot admin
- 使用open live writer客户端写博客(亲测有效)
- json数据前台解析 修改check属性用prop()
- Python 为threading.Thread添加 terminate
- JS语法字典---网友总结
- asp.net MVC 4.0 Controller回顾——ModelBinding实现过程