[leetcode] Min Stack @ Python
原题地址:https://oj.leetcode.com/problems/min-stack/
解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列。
代码:
class MinStack:
# @param x, an integer
def __init__(self):
self.stack1 = []
self.stack2 = []
# @return an integer
def push(self, x):
self.stack1.append(x)
if len(self.stack2) == 0 or x <= self.stack2[-1]:
self.stack2.append(x) # @return nothing
def pop(self):
top = self.stack1[-1]
self.stack1.pop()
if top == self.stack2[-1]:
self.stack2.pop() # @return an integer
def top(self):
return self.stack1[-1] # @return an integer
def getMin(self):
return self.stack2[-1]
[leetcode] Min Stack @ Python的更多相关文章
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- LeetCode: Min Stack 解题报告
Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrievi ...
- 【leetcode】Min Stack -- python版
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- [LeetCode] Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode——Min Stack
Description: Design a stack that supports push, pop, top, and retrieving the minimum element in cons ...
- LeetCode() Min Stack 不知道哪里不对,留待。
class MinStack { public: MinStack() { coll.resize(2); } void push(int x) { if(index == coll.size()-1 ...
- [LeetCode] Min Stack 栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode Min Stack 最小值栈
题意:实现栈的四个基本功能.要求:在get最小元素值时,复杂度O(1). 思路:链表直接实现.最快竟然还要61ms,醉了. class MinStack { public: MinStack(){ h ...
随机推荐
- ZOJ3772_Calculate the Function
给出一些数组a[i],每次询问为li,ri,定义f[li]=a[li],f[li+1]=a[li+1],对于其他不超过ri的位置,f[x]=f[x-1]+a[x]*f[x-2] . 题目有着浓浓的矩阵 ...
- 【半平面交】bzoj2618 [Cqoi2006]凸多边形
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define ...
- C++ 多重集的使用
C++ 多重集的使用 多重集当中的数据映射关系将不是前面的一对一的关系,而是一对多,也就是可以在容器当中插入具有相同key的实例.关于组织方式,LZ进行了下面的大胆的预测. 第一.底层的数据组织方式如 ...
- OpenSessionInViewFilter 的配置及作用
spring为我们解决hibernate的Session的关闭与开启问题. Hibernate 允许对关联对象.属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 Hibernate Sessio ...
- 1.1 Activity
1.概念 Application:由多个相关的松散的与用户进行交互Activity组成,通常被打包成apk后缀文件中: Activity:就是被用来进行与用户交互和用来与android内部特性交互的组 ...
- Lintcode 175 Invert Binary Tree
I did it in a recursive way. There is another iterative way to do it. I will come back at it later. ...
- MySQL去除外键关联关系
导数据或者删数据有主外键关联会特别麻烦,可以外键关联,数据处理完再加上. SET FOREIGN_KEY_CHECKS = 0; DELETE FROM frm_userinfo_; SET FORE ...
- 使用NHibernate(8)-- 延迟加载
1,延迟加载. 延迟加载,即用到的时候再加载数据.这种机制是非常有情怀的,比如一篇中的用户实体有标签.问题等导航属性,如果只是用到用户名去查询整个实体,则把相关的标签和问题也都加载,性能会比较低.而有 ...
- 解决JSON.stringify()在IE10下无法使用的问题
今天在IE10下遇到了JSON.stringify()无法使用的问题,错误信息为:'JSON' is undefined . 开始以为是没有添加json2.js引用的原因.后来发现,其他地方也没添加j ...
- 修改注册表来修改IE的设置---资料汇总
原文链接: http://blog.csdn.net/wangqiulin123456/article/details/17068649 附带批处理执行脚本: @echo off &title ...