Min Stack [LeetCode 155]
1- 问题描述
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
2- 思路分析
使用两个栈,一个栈保存原始数据(stack),另一个栈保持每个元素对应的最小元素(minStack)。如stack中从栈底到栈顶为[3, 1, 5, 7, 0],那么minStack中从栈底到栈顶为[3, 1, 1, 1, 0]。可参考[LeetCode] Min Stack in Python
3- Python实现
# -*- coding: utf-8 -*-
# Min Stack class MinStack:
def __init__(self):
self.stack = []
self.min = [] # 保存最小值 # @param x, an integer
# @return an integer
def push(self, x): # 入栈
self.stack.append(x)
if not self.min: # 如果min栈为空则直接入栈
self.min.append(x)
return x
if x < self.min[-1]: # 如果入栈元素x比min栈顶小,则x入栈
self.min.append(x)
else: # 如果x比min栈顶大,则重复栈顶元素
self.min.append(self.min[-1])
return x # @return nothing
def pop(self): #出栈
self.stack.pop()
self.min.pop() # min栈同步 # @return an integer
def top(self): #栈顶
return self.stack[-1] # @return an integer
def getMin(self): #栈内最小值
return self.min[-1]
Min Stack [LeetCode 155]的更多相关文章
- Min Stack leetcode
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Min Stack (LeetCode) tweak it to avoid Memory Limit Exceeded
class MinStack { public: void push(int x) { if(values.empty()) { values.push_back(x); min_indices.pu ...
- LeetCode 155:最小栈 Min Stack
LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...
- 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 ...
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
随机推荐
- ylbtech-dbs:ylbtech-4,PurpleHouse(房地产楼盘销售系统)
ylbtech-dbs:ylbtech-4,PurpleHouse(房地产楼盘销售系统) -- =============================================-- Crea ...
- JadClipse eclipse反编译插件
A.下载JadClipse,http://jadclipse.sourceforge.net/wiki/index.php/Main_Page#Download,注意选择与eclipse版本一致的版本 ...
- http头信息
请求头:用于告诉服务器,客户机支持的数据类型accept-charset:用于告诉服务器,客户机采用的编码accept-Encoding:用于告诉服务器,客户机支持的数据压缩格式Host:客户机通过这 ...
- 性能测试工具Gatling - 设置Recorder
Gatling自带的Recorder,可以大大节省我们书写scenario的时间. 用法和selenium的IDE类似,作为一个代理服务器在browser和application之间做桥梁作用 ...
- NLog文章系列——如何配置NLog(转)
NLog使用方法 作者:Jaros?aw Kowalski <> 翻译:CrazyCoder(由衷感谢他的热心!!) 原文:http://www.nlog-project.org/conf ...
- spring声明式事务
一.传统事务 二.通过spring配置完成事务: 1.配置spring,加入spring的jar包,加入spring的配置文件 2.配置数据源,这里使用c3p0,加入c3p0 jar包和mysql数据 ...
- [Java] 读写字节数据,过滤流DataOutputStream和DataInputStream
package test.stream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io ...
- feedback 是什么意思
feedback 是什么意思 能否不要说 feedback 呢? 加一个 feedback? 天啊 先解释一下 feedback 是什么 ? 还有 aria-describedby=&q ...
- select into from 和 insert into select 的用法和区别
select into from 和 insert into select都是用来复制表,两者的主要区别为: select into from 要求目标表不存在,因为在插入时会自动创建.insert ...
- T4 模板的调试方法,方便大家遇到问题自己快速定位和优化
T4 模板的调试方法,方便大家遇到问题自己快速定位和优化 :1. .ttinclude文件的第一行修改为 <#@ template language="C#" debug=& ...