leetcode-easy-design-155 Min Stack
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的更多相关文章
- <LeetCode OJ> 155. Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【leetcode❤python】 155. Min Stack
#-*- coding: UTF-8 -*- class MinStack(object): def __init__(self): """ ...
- 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 ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- LeetCode 155 Min Stack(最小栈)
翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【LeetCode】155. Min Stack 最小栈 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
- Java for LeetCode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
随机推荐
- ubuntu 创建定时备份pg数据库定时任务
ubuntu创建定时备份数据库定时任务 一.命令文件 创建db_back.sh #!/bin/bash echo "start backup" /usr/lib/postgresq ...
- 独热编码 pandas get_dummies
映射技巧 将'income_raw'编码成数字值 income_mapping = {'<=50K': 0,'>50K': 1} income = income_raw.map(incom ...
- 多线程的些许理解(平台x86,具体考虑linux,windows)
多线程的些许理解 一.体系架构 1.原子操作 1) 定义 不可中断的一个或者一系列操作,也就是不会被线程调度机制打断的操作,在运行期间不会有任何的上下文切换(context switch). 2) 我 ...
- Java入门指南-01 基本概要说明
一.Java语言概述 Java是一门面向对象编程语言.编程,即编写程序.程序对于我们来说,应该是有所了解的.只是有可能你们不知道而已.比如,我们电脑上的 QQ.谷歌浏览器等,都叫做应用程序. 二.本系 ...
- parfile解决exp时tables过多问题
parfile 一般用于表数据过大.使用导出.导入命令参数过多等场景: 在对oracle数据库使用exp命令导出数据时,如果tables=后面跟的表比较多,就是导致命令行放不下,从而不能导出.百度一把 ...
- 如何优雅高效的写博客(Sublime + Markdown + Evernote)
如何优雅高效的写博客(Sublime + Markdown + Evernote) 本文主要是参照了几位大神的博客加上自己捣鼓了半天,比较适合新手流畅阅读 非常感谢下面两位大神: @dc_726: h ...
- CentOS 7 安装 metasploit-framework
1 一键安装metasploit-framework apt-get install curl,wgetcurl https://raw.githubusercontent.com/rapid7/me ...
- jvm的几篇文章
https://www.cnblogs.com/xrq730/category/731395.html
- 利用java8新特性,用简洁高效的代码来实现一些数据处理
定义1个Apple对象: public class Apple { private Integer id; private String name; private BigDecim ...
- linux的逻辑运算符
1:expression :用于计算括号中的组合表达式,如果整个表达式的计算按结果为真,则测试结果也为真. 2:!exp:客队表达式进行逻辑非运算,即对测试结果求反 3:符合 -a 或者 && ...