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. oracle merge into用法

    转载:http://blog.163.com/duanpeng3@126/blog/static/885437352011724104741817/ 在 平时更新数据时,经常有这样一种更新,即将目标表 ...

  2. [译]在Node中使用indicative来验证model

    原文: http://amanvirk.me/validating-models-in-node-js-using-indicative/\ 在nodejs中有关于验证model的包有许多, 我最喜欢 ...

  3. soj 2013年 Nanjing Slection

    这样加边比STL快! 不明白为什么要+mod #include<iostream> #include<cstdio> #include<queue> #includ ...

  4. svn 版本转为git

    git clone 相当于git init 和 git svn fetch.git svn rease git svn fetch 从svn服务器取指定区间的版本转化成git库 git svn reb ...

  5. redis该如何分区-译文(原创)

    写在最前,最近一直在研究redis的使用,包括redis应用场景.性能优化.可行性.这是看到redis官网中一个链接,主要是讲解redis数据分区的,既然是官方推荐的,那我就翻译一下,与大家共享. P ...

  6. install alilang

    $sudo dpkg -i alilang.deb $ sudo alilang

  7. PROJ4初探(转并整理格式)

    PROJ4初探(转并整理格式) Proj4是一个免费的GIS工具,软件还称不上. 它专注于地图投影的表达,以及转换.采用一种非常简单明了的投影表达--PROJ4,比其它的投影定义简单,但很明显.很容易 ...

  8. window跳转页面

    1.直接的事件跳转 window.location.href="你所要跳转的页面"; 2.新窗口跳转 window.open('你所要跳转的页面'); 3.返回上一页 window ...

  9. Python dir

    1. 在python命令行交互环境下,可以用dir()函数查看当前的变量,比如: >>> dir()['__builtins__', '__doc__', '__loader__', ...

  10. MySQL 慢查询日志分析及可视化结果

    MySQL 慢查询日志分析及可视化结果 MySQL 慢查询日志分析 pt-query-digest分析慢查询日志 pt-query-digest --report slow.log 报告最近半个小时的 ...