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. for 与 for in

    在JavaScript中提供了两种方式迭代对象: (1)for 循环: (2)for..in循环: 使用for循环进行迭代数组对象,想必大家都已经司空见惯了.但是,使用for.. in循环时,大家可要 ...

  2. 二分图 最小点覆盖 poj 3041

    题目链接:Asteroids - POJ 3041 - Virtual Judge  https://vjudge.net/problem/POJ-3041 第一行输入一个n和一个m表示在n*n的网格 ...

  3. express 学习札记

    Enjoy yourself! 祝你玩得开心! I have no idea. 我没有头绪. I just made it! 我做到了!  I’ll see to it 我会留意的. Express ...

  4. ajax中的contendType和dataType知识点梳理

    在ajax中有2个参数比较重要,之前一直没有搞清楚,下面我们开始梳理一下 1.contentType字段:这个字段的意思,ajax发送给后端的数据是什么类型 如果在ajax中不指定这个属性,则默认是u ...

  5. day 13 模块

    模块 一个py文件,就是一个模块,一个模块是一些相似功能的集合体. # echo.py 定义一个模块. 下文都用这个 print('from echo 模块') name = 'echo' def f ...

  6. html5的鼠标拖拽

    鼠标拖拽 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  7. Aspose.words一 DOM结构

    2.文档对象模型概述 2.1 DOM介绍 Aspose.Words的文档对象模型(以下简称DOM)是一个Word文档在内存中的映射,Aspose.Words的DOM可以编程读取.操作和修改Word文档 ...

  8. tomcat/eclipse提速[z]

    在使用Eclipse开发项目过程中,一度使Eclipse陷入瘫痪状态,Tomcat启动项目时也异常缓慢,增加了超时限制并没有用,有时候项目根本运行不起来,简直让人崩溃,可能我电脑内存小(4G),配置低 ...

  9. asp.net core mvc 统一过滤参数,防止注入漏洞攻击

    参考链接: http://www.lanhusoft.com/Article/132.html 在core下,多少有些改动,其中js部分被注释掉了,如下: public static string F ...

  10. localstorage和vue结合使用2

    html <template> <div class="hello"> <div class="page-top"> < ...