题目来源:

  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. PhoneGap Xcode iOS教程

    http://mobile.51cto.com/web-334924.htmhttp://phonegap.com/install/http://www.phonegap100.com/jiaoche ...

  2. c#中的数据类型简介(数组)

    c#中的数据类型简介(数组) 数组定义 可以将数组看成相同数据类型的一组或多组数据,包括一维数组,多维数组和交错数组. 数值数组元素的默认值设置为零,而引用元素的默认值设置为 null. 交错数组是指 ...

  3. openwrt 家用备忘

  4. PHPMailer发送邮件方法

    /** * * 测试邮件发送s * @param 服务器 $Host * @param 端口 $Port * @param 昵称 $Fromname * @param 身份验证用户名 $Usernam ...

  5. ArrayList--卧槽这就是源码

    最近在<数据结构与算法分析(java版)>中看到了实现ArrayList的简易代码,亲自做了一下.个中的疑点还是在代码中实现了一下.其中就包括内部类Iterator对外部成员访问的问题. ...

  6. tornado web框架

    tornado web框架 tornado简介 1.tornado概述 Tornado就是我们在 FriendFeed 的 Web 服务器及其常用工具的开源版本.Tornado 和现在的主流 Web ...

  7. python----面向对象:1类的定义

    1.python中定义类的格式如下: class className(baseClassName): def functionName(argslist): 2.定义一个person类:它有一个Nam ...

  8. ODI Studio拓扑结构的创建与配置(Oracle)

    一.概念解释 Topology Manager主要用来管理下面5类任务,并将信息存储在主资料库中,供所有模块共享使用. 物理体系结构: 定义各种技术及其数据服务器.物理架构.物理代理.数据服务器瞎可以 ...

  9. 常用在线工具及API网址总结

    1.小图标在线查找 https://www.iconfinder.com/ 2.在线做图,Flowchart流程图,BPMN图,Org组织结构图等 http://www.processon.com/ ...

  10. Jdt Javax

    http://www.javablogging.com/dynamic-in-memory-compilation/ http://www.java2s.com/Code/Java/JDK-6/Com ...