155. Min Stack - Unsolved
https://leetcode.com/problems/min-stack/#/solutions
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- getMin() -- Retrieve the minimum element in the stack.
Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.
class MinStack(object): def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
# set two stacks
self.minStack = [] def push(self, x):
self.stack.append(x)
if len(self.minStack) and x == self.minStack[-1][0]:
self.minStack[-1] = (x, self.minStack[-1][1] + 1)
elif len(self.minStack) == 0 or x < self.minStack[-1][0]:
self.minStack.append((x, 1)) def pop(self):
#如果 栈顶值 == 最小值栈顶值
if self.top() == self.getMin():
#如果 最小值栈顶元素次数 > 1
if self.minStack[-1][1] > 1:
#最小值栈顶元素次数 - 1
self.minStack[-1] = (self.minStack[-1][0], self.minStack[-1][1] - 1)
else:
#最小值栈顶元素弹出
self.minStack.pop()
return self.stack.pop() def top(self):
return self.stack[-1] def getMin(self):
return self.minStack[-1][0] # 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()
Back - up knowledge:
Implementation of Stack
Some sols:
1
https://discuss.leetcode.com/topic/11985/my-python-solution
2
https://discuss.leetcode.com/topic/37294/python-one-stack-solution-without-linklist
3
http://bookshadow.com/weblog/2014/11/10/leetcode-min-stack/
4
http://www.aichengxu.com/data/720464.htm
5
155. Min Stack - Unsolved的更多相关文章
- 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 ...
- Java for LeetCode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【leetcode】155 - Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode题解 #155 Min Stack
写一个栈,支持push pop top getMin 难就难在在要在常量时间内返回最小的元素. 一开始乱想了很多东西,想到了HashMap,treeMap,堆什么的,都被自己一一否决了. 后来想到其实 ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LC 155 Min Stack
问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...
随机推荐
- CentOS服务器,大访问量会造成日志文件迅速增大,半年左右得清除一下
今天早上一打开QQ,一大群客户说我的网站登录不了,谷歌了一下,原来是服务器不能存储SESSION.后来问一下服务器托管商,被告知我的VPS运行的时间太久并且访问量很大,因此访问日志爆满. 服务器托管商 ...
- Zabbix 3.0编译安装
环境准备Centos 6.X 数据库准备默认centos yum源中mysql包的版本号为5.1,为了能使zabbix 3.0能达到最好的性能效果,安装最新版的mysql数据库. yum list i ...
- Shell教程 之test命令
Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值.字符和文件三个方面的测试. 1.数字测试 参数 说明 -eq 等于则为真 -ne 不等于则为真 -gt 大于则为真 -ge 大于 ...
- mybatis知识点(已掌握)
1.${} 和 #{} 的区别? ${} 直接显示传入数据,不能防止sql注入,一般用于传数据库对象(比如表名). #{} 传入数据被当成字符串,自动加上双引号,防止sql注入. 2.有哪些Execu ...
- 鼠标移上去触动hover致使div向上移动几个相素(动画transition轻轻的移动)
- Memcached学习一:Memcached安装使用
这篇博文以实用为目的,因此,先阐述如何安装Memcached,然后在实践中谈谈自己自己对Memcached的一点理解. 首先,安装Memcached,点击此处下载安装文件以及源码. 解压文件(我这里将 ...
- Mac下JDK、Maven、Tomcat、Git开发安装及环境变量配置
本文主要内容: - 1.Mac OS 10.11.6 [OS X EL Captain] - 2.Mac OS 安装Java 设置环境变量 - 3.Mac OS 安装Maven设置环境变量 - 4.M ...
- nginx安装扩展 sub_filter&http_ssl_module
nginx之所以轻,因为默认没有安装各种各样的扩展: nginx安装扩展插件: 下面列出两个可能会用到的插件 一.sub_filter内容过滤器,这个在nginx做http转发的时候会很常用 1.下载 ...
- ret和retf
ret指令用栈中的数据,修改IP的内容,从而实现近转移; retf指令用栈中的数据,修改CS和IP的内容,从而实现远转移. CPU执行ret指令时,进行下面两步操作: (IP) = ((ss)*16+ ...
- golang xml和json的解析与生成
golang中解析xml时我们通常会创建与之对应的结构体,一层层嵌套,完成复杂的xml解析. package main; import ( "encoding/xml" " ...