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 ...
随机推荐
- BufferedReader .BufferedWriter执行文本复制
/** * 需求:演示 BufferedReader 和 BufferedWriter 的使用,复制一个 java 文件 */ package cn.itcast.others.iostream; i ...
- 区间dp 51nod1021
题目链接:https://www.51nod.com/Challenge/ProblemSubmitDetail.html#!#judgeId=673021 代码: 参考博客:https://blog ...
- C++ 智能指针shared_ptr的实现
#include <memory> #include <iostream> using namespace std; template<typename T> cl ...
- iOS 打包方式
https://www.cnblogs.com/wengzilin/p/4601684.html
- java 基础之--反射详解
java 反射绝大部分都位于 java.lang.reflect package 中:常用的类就是: 1.class类:代表一个类 2.field类:代表类的成员变量 3.method:代表类的方法 ...
- es数组去重的简写
console.log([...new Set([2, 2, 12, 1, 2, 1, 6, 12, 13, 6])])
- php使用fputcsv进行大数据的导出
为了实验大数据的导出,我们这里先自已创建一张大表,表结构如下: CREATE TABLE `tb_users` ( `id` int(11) unsigned NOT NULL AUTO_INCREM ...
- [z] Git pull 强制覆盖本地文件
git fetch --all git reset --hard origin/master git pull git fetch 只是下载远程的库的内容,不做任何的合并 git reset 把HEA ...
- Java中重写与重载的区别
方法重载:关键字overload,方法名和方法的返回类型都相同,方法参数个数和类型不一样方法重写:也叫方法覆盖,关键字override,相对于类继承而言,重写的方法名,返回类型,参数个数,参数类型都要 ...
- 关于Vector,map等迭代器问题
vector.erase(it):后,it自动++,一定要弄清楚,删除成功后it指向删除的下一个地址. 对于map.erase(it),返回值为NULL,而Vector是返回itorator