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. vim常用命令笔记(转载)

    添加多行注释:   1. 首先按esc进入命令行模式下,按下Ctrl + v,进入列(也叫区块)模式;   2. 在行首使用上下键选择需要注释的多行;   3. 按下键盘(大写)“I”键,进入插入模式 ...

  2. Python标准库03 路径与文件 (os.path包, glob包)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 路径与文件的简介请参看Linux文件系统 os.path包 os.path包主要是 ...

  3. Python中将打印输出导向日志文件

    a. 利用sys.stdout将print行导向到你定义的日志文件中,例如: import sys # make a copy of original stdout route stdout_back ...

  4. Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  5. HttpClient和WebService的区别和介绍

    1. HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.  HttpClient用来调用服务,它是模拟 ...

  6. http 303 307 302 状态码理解

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

  7. zabbix如何监控WEB应用性能

    HTTP服务目前最流行的互联网应用之一,如何监控服务的健康状态对系统运维来说至关重要.   Zabbix本身提供了对WEB应用程序的监控,比如监控WEB程序的Download Speed,Respon ...

  8. node在安装完成后,出现node不是内部或外部命令

    node在安装完成后,查看node版本 node -v出现"node不是内部或外部命令"郁闷. 各种搜索之后,处理好了问题了. 一张图解决问题.

  9. 基于RDBMS的BI设计

    ================================ 都说BI,什么OLAP,什么ROLAP,MOLAP,但是如何基于RDBMS实现,基本都不怎么说. 怎么做的: 1.通过多维分析模型,存 ...

  10. VC ClistCtrl不同行背景色

    大家经常用到listctrl 做表格 为了好看,要给每行设置不同背景色.很实用的一个小技巧. 1:首先在.h里添加以下消息 afx_msg void OnDrawColorForMyList(NMHD ...