题目来源:

  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. q.js实现nodejs顺序调用

    nodejs的异步调用有时候是最让人头疼的,如何能是一些代码顺序的执行呢,这里和大家分享nodejs的promise 什么是promise promise一个标准,它描述了异步调用的返回结果,包括正确 ...

  2. 使用 Struts 2 实现国际化

    struts2国际化(I18N) 国际化也叫I18N,是Internationalization的简称.Struts2国际化是建立在Java国际化基础上,只是Struts2框架对Java国际化进行了进 ...

  3. 【转】unity3d的常用快捷键

    Unity3D默认的快捷键. shift +方向键             向“向方向键前进” Windows系统Unity3D中的快捷键 组合键 键 功能 File 文件 Ctrl   N New ...

  4. JFrame??

    swing的三个基本构造块:标签.按钮.文本字段.但需要个地方安放他们,并希望用户如何处理他们.JFrame类就是解决这个问题————它是一个容器,允许程序员把其他组件添加到它里面,把它们组织起来,并 ...

  5. java web每天定时执行任务(四步轻松搞定)

    第一步: package com.eh.util; import java.util.Calendar; import java.util.Date; import java.util.Timer; ...

  6. Nhibernate 映射关系,一对多 多对一与多对手在映射文件中的体现。

    今天做了第一个Nhibernate项目,摸着石头过河,学到了一些东西,在这里将自己总结体会到的一些映射关系写出来,与大家分享,由于是初学者,如果有不对的地方希望大家能够指出来. 首先要说明要建立的几张 ...

  7. jQuery 随滚动条滚动效果 (适用于内容页长文章)

    直接入题! 当内容页比较长的时候,网站右侧一直是空白,不如放点有用的东西给用户看,最好不要放广告,因为那样很邪恶,你懂的. 好吧,昨天写了这个东西,jQuery滚动随动区块,代码如下: //侧栏随动 ...

  8. Java数据结构习题_算法分析

    2.设T1(N)=O(f(N)),T2(N)=O(f(N)),则: T1(N)-T2(N)=o(f(N))           False,若1位2N,2为N T1(N)/T2(N)=O(1)     ...

  9. iOS中不透明度的查看

    模拟器工具条 Debug-->Color Blended Layers 即中文显示下 调试 -->颜色混合层 绿色代表不透明部分,红色代表透明部分,红色越多对性能影响越大

  10. SQL Server 数据的创建、增长、收缩

    第一步: create database Studio         on primary          (name = 'Studio',filename='E:\DB\Studio.mdf' ...