[LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack.
It should support push, pop and min operation all in O(1) cost.
Notice
min operation will never be called if there is no number in the stack.
push(1)
pop() // return 1
push(2)
push(3)
min() // return 2
push(1)
min() // return 1
LeetCode上的原题,请参见我之前的博客Min Stack.
解法一:
class MinStack {
public:
MinStack() {}
void push(int number) {
m1.push(number);
if (m2.empty() || m2.top() >= number) {
m2.push(number);
}
}
int pop() {
if (m1.empty()) return -;
int t = m1.top(); m1.pop();
if (!m2.empty() && m2.top() == t) m2.pop();
return t;
}
int min() {
if (!m2.empty()) return m2.top();
return -;
}
private:
stack<int> m1, m2;
};
解法二:
class MinStack {
public:
MinStack():mn(INT_MAX) {}
void push(int number) {
if (number <= mn) {
s.push(mn);
mn = number;
}
s.push(number);
}
int pop() {
int t = s.top(); s.pop();
if (t == mn) {
mn = s.top(); s.pop();
}
return t;
}
int min() {
return mn;
}
private:
int mn;
stack<int> s;
};
[LintCode] Min Stack 最小栈的更多相关文章
- [CareerCup] 3.2 Min Stack 最小栈
3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...
- lintcode 中等题:Min stack 最小栈
题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- [LeetCode] 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 ...
- 第30题:LeetCode155. Min Stack最小栈
设计一个支持 push,pop,top 操作,并能在O(1)时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素 ...
- 155 Min Stack 最小栈
设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈. push(x) -- 将元素x推入栈中. pop() -- 删除栈顶的元素. top() -- 获取 ...
- 【LeetCode】155. Min Stack 最小栈 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
- LintCode Min Stack
用两个stack, 第一个按顺序放所有值,第二个只放当前最小值. 注意: 1. 最小值有多个则都放到两个stack里, 尤其别忘放第二个: 2. pop时若两个stack的最上面值相等则都pop, 不 ...
随机推荐
- [Machine Learning & Algorithm] 决策树与迭代决策树(GBDT)
谈完数据结构中的树(详情见参照之前博文<数据结构中各种树>),我们来谈一谈机器学习算法中的各种树形算法,包括ID3.C4.5.CART以及基于集成思想的树模型Random Forest和G ...
- CSS背景background详解,background-position详解
背景(background)是css中一个重要的的部分,也是需要知道的css的基础知识之一.这篇文章将会涉及css背景(background)的基本用法,包括诸如 background-attachm ...
- C和指针 第十四章 预处理器 头文件
编写一个C程序,第一个步骤称为预处理,预处理在代码编译之前,进行一些文本性质的操作,删除注释.插入被include的文件.定义替换由#define定义的符号,以及确定代码的部分内容是否应该按照条件编译 ...
- 用JS做关灯游戏(初级)
这是一个很有意思的游戏,可以试着玩下. <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- iOS开发——高级篇——流水布局UICollectionViewFlowLayout的基本使用
之前看到过的一篇文章 对collectionView的使用总结的非常好:“iOS6新特征:UICollectionView介绍” 流水布局在现在的应用中很常见了,简单的研究了下,实现下面的功能 那我这 ...
- Ubuntu设置环境变量 16.04
打开终端并输入: sudo gedit /etc/environment. 2 输入用户密码.这时输入的密码是不可见的. 3 如图,在PATH="...."的末尾处添加: :/op ...
- SQL查询第m条到第n条的方法
SQL查询第m条到第n条的方法 如表名为GOOD Sselect top (n-m) * from GOODS where (某一列名) not in (select top m (某一列名) fro ...
- 微信分享ios设备没有分享图标安卓有分享图标 (分享功能没有问题)
解决方案:去除 var sharedata = { title: $("#shareTitle").val(), desc: $("#shareContent" ...
- 利用office2010 word2010生成目录
详细内容可以从以下链接下载: http://www.360disk.com/file-37040.html 从前一直用word的目录功能,觉得很方便.第一:可以在目录的首页通过Ctrl+鼠标单击左键可 ...
- PHP探针
来自LNMP.org 探针p.php 代码: <?php error_reporting(0); //抑制所有错误信息 @header("content-Type: text/html ...