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. 获取txt里面的内容

    import flash.net.URLLoader; import flash.net.URLRequest; import flash.events.Event; var txtLoad:URLL ...

  2. poj1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 题意:给定由0.1组成的数串长度n,询问次数m,每次询问给出a,b,s,表示区间[a,b]内1的数量为s(odd-奇数或even ...

  3. Mastering Unity 2D Game Development

    Mastering Unity 2D Game Development will give your game development skills a boost and help you begi ...

  4. 关于后台执行JS代码总结

    方法一.FineUI的 pagecontext对象 string js="functionName();"; PageContext.RegisterStartUpScript(j ...

  5. vue router返回上一页

    this.$router.go(-1) 不用router时,使用window.history.go(-1);

  6. 删除sslvpn用户

    config user localedit xinghen unset two-factornextend config user groupedit vpngroup unselect member ...

  7. mysql中的 随机字符串的生成

    方法1. SELECT SUBSTRING(MD5(RAND()),FLOOR(RAND()*26)+1,6) AS rand_str; 上诉示例产生的是:6位长度的随机字符串. 函数解释: rand ...

  8. java_5.1 for循环

    1.求1-100的和 public static void main(String[] args) { int sum = 0; for (int i = 0; i <= 100 ; i++) ...

  9. Bootstrap(6)辅组类和响应式工具

    一.辅助类 Bootstrap 在布局方面提供了一些细小的辅组样式,用于文字颜色以及背景色的设置.显示关闭图标等等. 1.情景文本颜色 各种色调的字体 <p class="text-m ...

  10. 免费证书https://lamp.sh/ssl.html

    https(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的 http 通道,简单讲是 http 的安全版.即 ht ...