[LeetCode]题解(python):155-Min Stack
题目来源:
https://leetcode.com/problems/min-stack/
题意分析:
实现一个小的栈,包括初始化,push,pop,top,和getMin。
题目思路:
思路是用两个数组来处理。
代码(python):
class MinStack(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.stack1 = []
self.stack2 = [] def push(self, x):
"""
:type x: int
:rtype: nothing
"""
self.stack1.append(x)
if len(self.stack2) == 0 or x <= self.stack2[-1]:
self.stack2.append(x) def pop(self):
"""
:rtype: nothing
"""
tmp = self.stack1.pop()
if tmp == self.stack2[-1]:
self.stack2.pop() def top(self):
"""
:rtype: int
"""
return self.stack1[-1] def getMin(self):
"""
:rtype: int
"""
return self.stack2[-1]
[LeetCode]题解(python):155-Min Stack的更多相关文章
- <LeetCode OJ> 155. Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- 【LeetCode】155. Min Stack 最小栈 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
- LeetCode题解 #155 Min Stack
写一个栈,支持push pop top getMin 难就难在在要在常量时间内返回最小的元素. 一开始乱想了很多东西,想到了HashMap,treeMap,堆什么的,都被自己一一否决了. 后来想到其实 ...
- 【leetcode❤python】 155. Min Stack
#-*- coding: UTF-8 -*- class MinStack(object): def __init__(self): """ ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Java for LeetCode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
随机推荐
- 通过WebService跨平台上传大文件到服务器
好长时间没写博客了,对最近工作中遇到的大文件上传和下载的代码贴出来和大家分享一下. 大文件的上传的和下载是C++的平台通过调用WebService实现文件上传和下载到服务器 /// <summa ...
- ubuntu下nvm,node以及npm的安装与使用
一:安装nvm 首先下载nvm,这里我们需要使用git,如果没有安装git,可以使用 sudo apt-get install git 来安装 git clone https://github.com ...
- C#l连接OPC进行数据交互
步骤 :引用 OPCNETAPI.DLL&&OPCNETAPI.COM.DLL 1.查询服务器 2. 连接服务器 3. 读取数据 4.写入数据 1.查询服务器 :根 ...
- VS2013关于“当前不会命中断点源代码与原始版本不同”的BUG
文件明明没有动过,竟然一直给我这种提示! 解决方法:将出问题的文件用notepad打开,然后另存为Unicode编码,覆盖原来的文件. 网上另外有一种办法是:通过重新格式化出问题的源文件亦可以解决,即 ...
- 解决Linux文档显示中文乱码问题以及编码转换
解决Linux文档显示中文乱码问题以及编码转换 解决Linux文档显示中文乱码问题以及编码转换 使vi支持GBK编码 由于Windows下默认编码是GBK,而linux下的默认编码是UTF-8,所以打 ...
- 关于Webapp导航设计的思考
一.马蜂窝 http://m.mafengwo.com
- 安卓linearlayout布局的一个嵌套实例
XML代码如下 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...
- SSH登陆服务器的简单命令
SSh 用户名@目标Ip: 回车输入密码:
- 在CGridView调用CJuiDialog的弹出层
<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'user-grid', 'dataProv ...
- Minimum Transport Cost(floyd+二维数组记录路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...