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. Unity自动打包 apk

    1.流程 Unity打包 apk,会把Unity安装目录下的默认 AndroidManifest.Xml 文件覆盖到apk中去,同时还会拷贝该文件所在目录下的其它默认设置文件,如 res 和 asse ...

  2. LYDSY热身赛 escape

    Description 给出数字N(1<=N<=10000),X(1<=x<=1000),Y(1<=Y<=1000),代表有N个敌人分布一个X行Y列的矩阵上矩形的行 ...

  3. ASP.NET学习链接

    张子阳个人ASP.NET技术博客:http://www.tracefact.net/Asp-Net/ 动态加载asp.net分页控件:http://www.cnblogs.com/cresuccess ...

  4. [Machine Learning] Learning to rank算法简介

    声明:以下内容根据潘的博客和crackcell's dustbin进行整理,尊重原著,向两位作者致谢! 1 现有的排序模型 排序(Ranking)一直是信息检索的核心研究问题,有大量的成熟的方法,主要 ...

  5. 安卓APP关于切图标

    bin res drawable-hdpi drawable-ldpi drawable-mdpi drawable-nodpi drawable-xhdpi drawable-xxhdpi x越大代 ...

  6. php之jquery

    <!DOCTYPE html> <html> <head> <script type="xxx.js"></script> ...

  7. JS常见错误和分析

    列举一些在前端开发中遇到的一些错误信息和解决办法 错误信息 解决办法 Uncaught SyntaxError: Unexpected token o 使传入的字符串不是符合严格的JSON格式会抛出异 ...

  8. TypeError: datetime.datetime(2016, 9, 25, 21, 12, 19, 135649) is not JSON serializable解决办法

    1.一个简单的方法来修补json模块,这样序列将支持日期时间. import json import datetime json.JSONEncoder.default = lambda self, ...

  9. 8.7 jquery-dom manipulation

    // 获得设定内容 [text(),html(),val()]; // 获得设定属性 [attr(),removeAttr()]; // 获得设定 css class [addClass,remove ...

  10. AngularJS常用指令

    一.指令 1.ng-app 定义应用程序的根元素 <div ng-app="app"></div> var app = angular.module('ap ...