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.

Have you met this question in a real interview?

Yes
Example

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 最小栈的更多相关文章

  1. [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 ...

  2. lintcode 中等题:Min stack 最小栈

    题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...

  3. [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速

    题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...

  4. [LeetCode] Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  5. [LeetCode] 155. Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  6. 第30题:LeetCode155. Min Stack最小栈

    设计一个支持 push,pop,top 操作,并能在O(1)时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素 ...

  7. 155 Min Stack 最小栈

    设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈.    push(x) -- 将元素x推入栈中.    pop() -- 删除栈顶的元素.    top() -- 获取 ...

  8. 【LeetCode】155. Min Stack 最小栈 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...

  9. LintCode Min Stack

    用两个stack, 第一个按顺序放所有值,第二个只放当前最小值. 注意: 1. 最小值有多个则都放到两个stack里, 尤其别忘放第二个: 2. pop时若两个stack的最上面值相等则都pop, 不 ...

随机推荐

  1. Bash 中为 _ 变量赋空值的三个场景

    $_ 有好几个功能,我们最常用的是用它来获取“刚刚执行过的命令的最后一个参数”这个功能,比如下面这样: $ ls ~/Downloads/very/long/dir/  # ls 到某个目录看看有没有 ...

  2. 大熊君JavaScript插件化开发------(实战篇之DXJ UI ------ ProcessBar)

    一,开篇分析 Hi,大家好!大熊君又和大家见面了,还记得前两篇文章吗.主要讲述了以“jQuery的方式如何开发插件”,以及过程化设计与面向对象思想设计相结合的方式是 如何设计一个插件的,两种方式各有利 ...

  3. Git 简明教程

    其他Git资料: Git Community Book 中文版

  4. 页面localStorage用作数据缓存的简易封装

    最近做了一些前端控件的封装,需要用到数据本地存储,开始采用cookie,发现很容易就超过了cookie的容量限制,于是改用localStorage,但localStorage过于简单,没有任何管理和限 ...

  5. Mybatis在insert操作时返回主键

    今天在写项目的时候,遇到一个需求,就是在像数据库插入数据的时候,要保留插入数据的主键,以便后续进行级联时,可以将该主键作为另张表的外键. 但是在正常情况下我们使用插入语句返回的是int型,含义是影响该 ...

  6. Linux压缩和解压汇总

    各种后缀的压缩包压缩方法和解压方法 压缩包 压缩 解压 .tar.gz和.tgz tar -czf jpg.tar.gz *.jpg tar -xzvf filename -C path .tar.b ...

  7. js实现四舍六入 奇进偶舍

    function PointFloat(src, pos) { return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos); } // ...

  8. POJ 2337 Catenyms(有向图的欧拉通路)

    题意:给n个字符串(3<=n<=1000),当字符串str[i]的尾字符与str[j]的首字符一样时,可用dot连接.判断用所有字符串一次且仅一次,连接成一串.若可以,输出答案的最小字典序 ...

  9. hadoop 笔记

    我们常说的分布式系统,其实就是分布式软件系统,支持分布式处理的软件系统.他是在通信网络互联的多处理机体系结构上执行任务.   hadoop是分布式软件系统中文件系统层的软件,他实现了分布式文件系统和部 ...

  10. devexpress layoutcontrol 自动生成布局示例代码

    foreach (var row in lst.Select(x => x.crow).Distinct()) { LayoutControlItem layout_preitem = null ...