155. Min Stack - Unsolved
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.
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()
Back - up knowledge:
Implementation of Stack
Some sols:
1
https://discuss.leetcode.com/topic/11985/my-python-solution
2
https://discuss.leetcode.com/topic/37294/python-one-stack-solution-without-linklist
3
http://bookshadow.com/weblog/2014/11/10/leetcode-min-stack/
4
http://www.aichengxu.com/data/720464.htm
5
155. Min Stack - Unsolved的更多相关文章
- 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 ...
- Java for 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
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode题解 #155 Min Stack
写一个栈,支持push pop top getMin 难就难在在要在常量时间内返回最小的元素. 一开始乱想了很多东西,想到了HashMap,treeMap,堆什么的,都被自己一一否决了. 后来想到其实 ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LC 155 Min Stack
问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...
随机推荐
- git图片
- 二叉树的深度优先遍历与广度优先遍历 [ C++ 实现 ]
深度优先搜索算法(Depth First Search),是搜索算法的一种.是沿着树的深度遍历树的节点,尽可能深的搜索树的分支. 当节点v的所有边都己被探寻过,搜索将回溯到发现节点v的那条边的起始节点 ...
- HDU-1004.Let the ballon Rise(STL-map)
2019-02-28-08:56:03 初次做本题是用字符串硬钢,最近校队训练时又遇到才知道用map是真的舒服.需要注意的是map的用法. clear : 清除map中的所有元素,map.clear( ...
- 快速了解和使用Photon Server
https://blog.csdn.net/qq_36565626/article/details/78710787
- 不要62(数位DP)
不要62 http://acm.hdu.edu.cn/showproblem.php?pid=2089 Time Limit: 1000/1000 MS (Java/Others) Memory ...
- Python常用库大全,看看有没有你需要的
作者:史豹链接:https://www.zhihu.com/question/20501628/answer/223340838来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- 问题1:jquery实现全选功能,第二次失效(已解决)
问题:使用了attr("checked",true”)设置子复选框的被选状态,第一次执行功能正常,但第二次失效. 解决方案:将attr("checked",tr ...
- [Java学习]面向对象-多态
多态 多态发生条件 发生在有继承关系的类型中. 向上转型(自动类型转换)与向下转型(强制类型转换) //向上转型 //编译阶段a1被编译器看作是Animal类型,所以a1引用绑定的是Animal类中的 ...
- 【网络编程一】主机字节序与网络字节序以及ip地址转换函数
在计算机设计之初,对内存中数据的处理也有不同的方式,(低位数据存储在低位地址处或者高位数据存储在低位地址处),然而,在通信的过程中(ISO/OSI模型和TCP/IP四层模型中),数据被一步步封装(然后 ...
- Django xadmin 根据登录用户过滤数据
在adminx.py文件对应的的class中添加如下代码: def queryset(self): qs = super(taskAdmin, self).queryset() if self.req ...