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]的更多相关文章

  1. Min Stack leetcode

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  2. 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 ...

  3. LeetCode 155:最小栈 Min Stack

    LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...

  4. 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 ...

  5. leetcode 155. Min Stack --------- java

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  6. Java [Leetcode 155]Min Stack

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

  7. LeetCode算法题-Min Stack(Java实现)

    这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...

  8. 155. Min Stack

    题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...

  9. leetCode Min Stack解决共享

    原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...

随机推荐

  1. js swipe 图片滑动控件实现 任意尺寸适用任意屏幕

    http://www.swiper.com.cn/http://www.idangero.us/swiper/demos/ 解决问题点: 1.先得到图片真实的宽高, 根据真实宽高 等比例 2.调用的控 ...

  2. Linux中记录终端(Terminal)输出到文本文件(转载)

    一,如何把命令运行的结果保存到文件当中? 这个问题太简单了,大家都知道,用 > 把输出转向就可以了 例子: [lhd@hongdi ~]$ ls > ls.txt [lhd@hongdi ...

  3. java中的IO整理

    写在前面:本文章基本覆盖了java IO的全部内容,java新IO没有涉及,因为我想和这个分开,以突出那个的重要性,新IO哪一篇文章还没有开始写,估计很快就能和大家见面.照旧,文章依旧以例子为主,因为 ...

  4. 谈Objective-C Block的实现

    来源:http://blog.devtang.com/blog/2013/07/28/a-look-inside-blocks/ 前言 这里有关于block的5道测试题,建议你阅读本文之前先做一下测试 ...

  5. http 303 307 302 状态码理解

    最近在看 <<the rails4 way>> 书中提到了这几个状态码,网上搜到几篇文章 http://www.cnblogs.com/cswuyg/p/3871976.htm ...

  6. UBUNTU查看软件版本

    1.查看已安装软件版本aptitude show softwarename 2.查看软件安装目录dpkg -L softwarename

  7. [Flex] ButtonBar系列——如何给ButtonBar添加一个ViewStack

    <?xml version="1.0" encoding="utf-8"?> <!--如何给ButtonBar添加一个ViewStack--& ...

  8. Matla学习:figure+axes+plot

    function fig = SetDrawParam() %.获得屏幕尺寸 figpos = , 'ScreenSize');%获得屏幕尺寸,单位像素 %.设置坐标系在画布中的位置,针对不同尺寸或不 ...

  9. FreeMarker中List排序

    有时候需要在页面上对list排序,虽然也可以在后台代码中完成,但这个可能要看具体情况.排序的样本代码如下: <#list resultMap.topViewList?sort_by(" ...

  10. php分享表单提交到本页的实例

    我们在做表单提交时,一般都要设置表单的action属性,改属性用于指定表单提交到服务器上的哪个页面进行处理,但为空时,表示提交到本页进行处理,即提交给自己.本文章向大家分享表单提交给本页的实例. 实例 ...