mycode   21.48%

class MinStack(object):

    def __init__(self):
"""
initialize your data structure here.
"""
self.res = [] def push(self, x):
"""
:type x: int
:rtype: None
"""
self.res.append(x) def pop(self):
"""
:rtype: None
"""
if not self.res:
return None
else:
val = self.res[-1]
self.res[:] = self.res[:-1]
return val def top(self):
"""
:rtype: int
"""
if not self.res:
return None
else:
return self.res[-1] def getMin(self):
"""
:rtype: int
"""
if not self.res:
return None
else:
return min(self.res) # Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()

列表是有pop操作的

self.res.pop()

参考

始终更新最小值,所以节省 了时间

class MinStack(object):

    def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
self.minval = 99999999999999999 def push(self, x):
"""
:type x: int
:rtype: None
"""
if(x < self.minval):
self.minval = x
self.stack.append(x) def pop(self):
"""
:rtype: None
"""
if(self.stack[-1] == self.minval):
self.minval = 99999999999
for num in self.stack[0:-1]:
if(num < self.minval):
self.minval = num
self.stack.pop(-1) def top(self):
"""
:rtype: int
"""
return self.stack[-1] def getMin(self):
"""
:rtype: int
"""
return self.minval # Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()

leetcode-easy-design-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❤python】 155. Min Stack

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

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

  4. leetcode 155. Min Stack --------- java

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

  5. Java [Leetcode 155]Min Stack

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

  6. 155. Min Stack

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

  7. LeetCode 155 Min Stack(最小栈)

    翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...

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

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

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

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

  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. TVM:一个端到端的用于开发深度学习负载以适应多种硬件平台的IR栈

    TVM:一个端到端的用于开发深度学习负载以适应多种硬件平台的IR栈  本文对TVM的论文进行了翻译整理 深度学习如今无处不在且必不可少.这次创新部分得益于可扩展的深度学习系统,比如 TensorFlo ...

  2. sftp及两种连接模式简介

    sftp是ssh内含的协议,只要sshd服务器启动了,它就可用,它本身不需要ftp服务器启动. FTP服务器和客户端要进行文件传输,就需要通过端口来进行.FTP协议需要的端口一般包括两种: 控制链路- ...

  3. C++ Concurrency In Action 一些重点

    全部来自于gitbook  C++并发编程(中文版) 需要对一个还未销毁的std::thread对象使用join()或detach().如果想要分离一个线程,可以在线程启动后,直接使用detach() ...

  4. 标准C语言(11)

    多文件编程时一个文件里可以包含多个函数,一个函数只能属于一个文件 /* * 多文件编程演示 * */ #include <stdio.h> #include "01add.h&q ...

  5. java同步锁实现方法

    1.synchronized关键字修饰 当用此关键字修饰方法时,     内置锁会保护整个方法.在调用该方法前,需要获得内置锁,否则就处于阻塞状态 synchronized关键字也可以修饰静态方法,此 ...

  6. constant read 和 current read

    来自网络,并且在本机实验完成: onsistent read :我的理解,就是通过scn来读取.  读取的过程中要保证 scn是一致的.举个例子,一个SELECT 语句在SCN=100的时刻开始读取一 ...

  7. nginx第六天

    nginx正向代理 反向代理 Nginx正向代理配置 Nginx正向代理使用场景并不多见. 需求场景1: 如果在机房中,只有一台机器可以联网,其他机器只有内网,内网的机器想用使用yum安装软件包,在能 ...

  8. CSS3背景定位 background-origin

    background-size 大小 语法 background-size :[ <length> | <percentage> | auto ]{1,2} | cover | ...

  9. Python CGI编程Ⅲ

    GET和POST方法 浏览器客户端通过两种方法向服务器传递https://www.xuanhe.net/信息,这两种方法就是 GET 方法和 POST 方法. 使用GET方法传输数据 GET方法发送编 ...

  10. css-动画,a标签下,文字加下划线,而且动画是由中间向两边扩展开

    效果: html: <div class="warp"> <a class="welcome">期待您的参与</a> < ...