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 ...
随机推荐
- spring ioc原理(看完后大家可以自己写一个spring)
控制反转/依赖注入 最近,买了本Spring入门书:spring In Action .大致浏览了下感觉还不错.就是入门了点.Manning的书还是不错的,我虽然不像哪些只看Manning书的人那样专 ...
- ThinkPHP CURD返回结果参考
ThinkPHP CURD返回结果参考: 1)查询$table->find() ##返回一条记录,是一个关联数组,是一维数组.$table->select() ##返回第一维是索引数组,第 ...
- NAND flash cache编程
PROGRAM PAGE CACHE MODE 0x80-0x15: CACHE编程实际上是标准的页编程命令的带缓冲编程模式,编程开始是发布SERIAL DATA INPUT(0x80)命令,随后是5 ...
- JAVA中压缩与解压缩
以压缩Zip文件为例.主要是通过ZipOutputStream类实现.解压缩主要使用ZipFile类和ZipInputStream以及ZipEntry类. package main; import j ...
- 查看django里所有的url
>>> from django.core.urlresolvers import get_resolver >>> get_resolver(None).rever ...
- POJ 2823【单调队列】
题意: 给出序列,找出每个连续长度为k的子序列的最大值和最小值. 思路: 裸单调队列... 单调队列这东西用的真的非常局限,大概只能用到这种情景中== 简单说一下维护: 添加元素,为了保持单调性,排除 ...
- [HDU 2126] Buy the souvenirs (动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2126 题意:给你n个物品,m元钱,问你最多能买个多少物品,并且有多少种解决方案. 一开始想到的是,先解 ...
- DNS加速
http://elingwange.iteye.com/blog/1563497 http://blog.csdn.net/lize1988/article/details/10404645 java ...
- Hdfs增量导入小文件合并的思路
1.使用mr进行合并 2.使用getmerge 将文件拉取到本地,再上传到hdfs,注意nl参数 3.使用appendToFile 4.使用hadoop提供的打包压缩技术 Usage: hadoop ...
- css margin-top设置html元素之间的距离
css margin-top属性设置的是一个元素的顶端与另一个元素之间的距离.这个距离称为上外边距,本文章向大家介绍css margin-top属性的用法和实例,需要的朋友可以参考一下. css ma ...