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 ...
随机推荐
- git 空提交和重置提交者(转载)
From:http://www.xiukun.me/git%E4%BD%BF%E7%94%A8-allow-empty-%E8%BF%9B%E8%A1%8C%E7%A9%BA%E7%99%BD%E6% ...
- NSMutableAttributedString 设置不同颜色,不同字体的String
UILabel *infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(95, 20, 190, 70)]; infoLabel.backgroun ...
- IronPython调用C# DLL函数方法
C# DLL源码 using System; using System.Collections.Generic; using System.Text; using System.Security.Cr ...
- solr学习之添加文档
一.开篇语 其实Solr就是一个你可以通过他来查询文档的东西,他整个都是基于Document的,那么这些Document从何而来列? 当然是我们给他,而这些来源就包括了:数据库文件,XML,Json ...
- Wix安装包权限问题
Wix在安装完成之后,如果遇到非管理员用户(域用户或Win7+系统,UAC权限问题等),修改配置文件(setting.ini)文件时,会遇到文件权限为只读,无法修改问题: 解决方案有两种: 首先添加U ...
- orientationchange不管用啊
首先引入JQuery Mobile包,将 <script> //手持设备方向改变时执行 $(window).bind( 'orientationchange', function(e){ ...
- jquery怎么实现跨域的访问呢?与别人提供的接口连接
下面这个例子你可以参考下 <script> $.ajax({ async:false, url: 'http://www.mysite.com/demo.do', // 跨域URL ty ...
- Html5中的跨页面消息传输
1.如果要接受从其他的窗口那里发过来的消息,就必须对窗口对象的message事件进行监控. window.addEventListener("message",function() ...
- nyoj 76 超级台阶
点击打开链接 超级台阶 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 有一楼梯共m级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第m级,共有多少走法? 注:规 ...
- 20. Valid Parentheses(stack)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...