题目来源:

  https://leetcode.com/problems/min-stack/


题意分析:

  实现一个小的栈,包括初始化,push,pop,top,和getMin。


题目思路:

  思路是用两个数组来处理。


代码(python):

 class MinStack(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.stack1 = []
self.stack2 = [] def push(self, x):
"""
:type x: int
:rtype: nothing
"""
self.stack1.append(x)
if len(self.stack2) == 0 or x <= self.stack2[-1]:
self.stack2.append(x) def pop(self):
"""
:rtype: nothing
"""
tmp = self.stack1.pop()
if tmp == self.stack2[-1]:
self.stack2.pop() def top(self):
"""
:rtype: int
"""
return self.stack1[-1] def getMin(self):
"""
:rtype: int
"""
return self.stack2[-1]

[LeetCode]题解(python):155-Min Stack的更多相关文章

  1. &lt;LeetCode OJ&gt; 155. Min Stack

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

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

  3. leetcode 155. Min Stack --------- java

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

  4. Java [Leetcode 155]Min Stack

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

  5. 155. Min Stack

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

  6. 【LeetCode】155. Min Stack 最小栈 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...

  7. LeetCode题解 #155 Min Stack

    写一个栈,支持push pop top getMin 难就难在在要在常量时间内返回最小的元素. 一开始乱想了很多东西,想到了HashMap,treeMap,堆什么的,都被自己一一否决了. 后来想到其实 ...

  8. 【leetcode❤python】 155. Min Stack

    #-*- coding: UTF-8 -*- class MinStack(object):    def __init__(self):        """      ...

  9. [LeetCode] 155. Min Stack 最小栈

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

  10. Java for LeetCode 155 Min Stack

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

随机推荐

  1. HTML——框架

    1.frameset <html> <frameset cols="25%,50%,25%"> <frame src="frame_a.ht ...

  2. mysql错误号码:1129

    mysql 错误号码1129: mysql error 1129: Host 'bio.chip.org' is blocked because of many connection errors; ...

  3. android 7.0带来的

    Android 7.0 给开发者带来了什么 新的 Andorid N (Andorid 7.0)预览版发布了,但是新的Android预览版需要我们在已存在的APP上测试几乎全部内容,包括不同种类的屏幕 ...

  4. TCP/IP详解之:UDP协议

    第11章 UDP协议  UDP首部 UDP的检验和是可选的,而TCP的检验和是必须的: UDP的检验和是端到端的检验和.由发送端计算,由接收端验证: 尽管UDP的检验和是可选的,但总是推荐被使用 IP ...

  5. Qt使用快捷键

    在说快捷键之前先来说一个QtCreator调试的过程中经常发生的一个问题. 问题描述: 用QtCreator建立了一个纯C++的项目,但是在F5调试时,竟然提示ptrace不允许的操作,修改工程配置为 ...

  6. 寻求c++解答如下三个题目!

  7. 更改linux系统提示信息

    一个好的习惯会让人终生受益,当然我们做运维也是如此,比如我们在前期维护系统时修改或者删除掉/etc/issue /etc/issue.net这两个文件. 修改和删除的目的是屏蔽系统版本信息,这样是为了 ...

  8. Android 自定义shape圆形按钮

    Shape的属性: solid 描述:内部填充 属性:android:color 填充颜色 size 描述:大小 属性: android:width 宽 android:height 高 gradie ...

  9. Python中关于try...finally的一些疑问

    最近看Vamei的Python文章,其中一篇讲异常处理的,原本看完没啥疑惑,或许是自己想的简单了. 看到评论,一个园友的问题引起我的兴趣. 他的问题是 def func(x): try: return ...

  10. linux第七章《档案与目录管理》重点回顾