[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 ...
随机推荐
- 团队博客作业Week1
Study the projects done by previous student groups - View their blog site, use their software, email ...
- P1403约数研究
洛谷1403 约数研究 题目描述 科学家们在Samuel星球上的探险得到了丰富的能源储备,这使得空间站中大型计算机"Samuel2"的长时间运算成为了可能.由于在去年一年的辛苦工作 ...
- Spring MVC学习初篇
Spring mvc 使用配置: <!-- 使用MVC --> <servlet> <servlet-name>defaultDispatcher</serv ...
- 【数位dp】bzoj2089 不要62
http://www.cnblogs.com/xiaohongmao/p/3473599.html #include<cstdio> using namespace std; int n, ...
- Java里的if else嵌套语句例子
import java.util.Scanner; public class if_else3 { public static void main(String[] args) { Scanner s ...
- 命令行导入SQL文件
摘要:把数据库导出为XX.sql格式的数据库文件,导入到另外一个数据库中的时候,总是无法全部导入.及时用mysql的命令界面导入依然是无法全部导入.老师告诉我:在命令行中运行的效率和成功率是最快和最高 ...
- SQLite主键自增需要设置为integer PRIMARY KEY
按照正常的SQL语句,创建一个数据表,并设置主键是这样的语句: ), EventType )) 但使用这种办法,在SQLite中创建的的数据表,如果使用Insert语句插入记录,如下语句: INSER ...
- Windows上成功编译CoreCLR源代码
昨天得知微软在GitHub上发布CoreCLR的源代码之后,立马从GitHub上签出代码,并尝试在Windows Server 2012上进行编译. 参考CoreCLR的开发者指南(Developer ...
- 消灭ASP.NET CachedPathData.ValidatePath引起的HttpException异常
在博客程序的日志中经常会出现这样的错误日志: Url: http://www.cnblogs.com/cmt/p/sokcet_memory_leak.html (这个URL仅是示例)UserAgen ...
- [ACM_暴力] ZOJ 3710 [Friends 共同认识 最终认识 暴力]
Alice lives in the country where people like to make friends. The friendship is bidirectional and if ...