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

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

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • getMin() -- Retrieve the minimum element in the stack.

Example:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.
 
Sol:
 
 AC
 
class MinStack(object):

    def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
# set two stacks
self.minStack = [] def push(self, x):
self.stack.append(x)
if len(self.minStack) and x == self.minStack[-1][0]:
self.minStack[-1] = (x, self.minStack[-1][1] + 1)
elif len(self.minStack) == 0 or x < self.minStack[-1][0]:
self.minStack.append((x, 1)) def pop(self):
#如果 栈顶值 == 最小值栈顶值
if self.top() == self.getMin():
#如果 最小值栈顶元素次数 > 1
if self.minStack[-1][1] > 1:
#最小值栈顶元素次数 - 1
self.minStack[-1] = (self.minStack[-1][0], self.minStack[-1][1] - 1)
else:
#最小值栈顶元素弹出
self.minStack.pop()
return self.stack.pop() def top(self):
return self.stack[-1] def getMin(self):
return self.minStack[-1][0] # 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()
 

155. Min Stack - Unsolved的更多相关文章

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

  2. leetcode 155. Min Stack --------- java

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

  3. Java [Leetcode 155]Min Stack

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

  4. 155. Min Stack

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

  5. Java for LeetCode 155 Min Stack

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

  6. 【leetcode】155 - Min Stack

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

  7. LeetCode题解 #155 Min Stack

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

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

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

  9. LC 155 Min Stack

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

随机推荐

  1. Linux之须知

    1.服务器有哪些硬件? 主板,cpu,显卡,内存,硬盘,声卡,网卡 2.内存,CPU,硬盘的作用? cpu是管理与运算.内存:cpu和磁盘之间的缓冲设备,服务器关闭后,数据从内存中释放掉. CPU,电 ...

  2. 【模型】Toon Dragon

    下载地址:点击下载

  3. Mac 查看端口占用及杀死进程

    lsof -i : kill -

  4. 微信小程序开发——开发者工具无法输入中文的处理

    问题模块 框架类型 问题类型 操作系统 工具版本 开发者工具 小程序 Bug Windows v.02.1810290 异常描述: 无法输入中文,偶现,但是概率有点高,重启,重装,更新版本等等都未解决 ...

  5. ajax请求跨域

    解决方式 1: 解决方式 2: 服务端: package ceshi_utils; import java.util.*; import com.xwhb.utils.encrypt.CipherUt ...

  6. ubuntu下nginx的常用操作

    1.安装不多说了,我是使用apt-get进行安装的,直接键入 apt-get install nginx 2.启动nginx. 首先找到nginx的主运行程序(相当于windows下的nginx.ex ...

  7. java 图片的自定义大小

    java 小功能,进行图片的自定义大小 package com.project.video.controller; import java.awt.Color; import java.awt.Gra ...

  8. 15-算法训练 P1103

    http://lx.lanqiao.cn/problem.page?gpid=T372   算法训练 P1103   时间限制:1.0s   内存限制:256.0MB      编程实现两个复数的运算 ...

  9. linux下查看项目端口号,杀掉对应端口号的方法

    查看端口号:netstat -anp 结束端口号:sudo iptables -A INPUT -p tcp --dport 8012 -j DROP"

  10. __block的初步用法

    再block中使用 self 时,要在前面加上__block. 防止在block中用到self时把self对象retain, 造成内存泄露. __block UIViewController *saf ...