题目地址:

https://oj.leetcode.com/problems/min-stack/

题目内容:

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.

方法:

思路很简单。

维护两个栈,一个是数据栈正常进出;另一个最小栈的栈顶保存着当前数据栈中的最小值。

这么说可能看不太懂,举个例子吧。

数据栈中:9,9,3,4,4,4,5,3,3,10 其中10是栈顶。

最小栈中:9,9,3,3,3

push操作:

当前进入数据栈中的值小于等于最小栈栈顶的值的话,则也将该值压进最小栈,否则就只push数据栈。

pop操作:

如果当前数据栈栈顶的值等于最小栈栈顶的值,那么两个栈都弹值;否则只弹数据栈栈顶的值。

注意:C++中,如果使用vector,就会出现MLE错误。我换成stack就顺利过关。怀疑跟stack底层是个链表,而vector底层是动态增长数组有关。(剖析STL没看,SHIT,瞎猜一个)

全部AC代码:

class MinStack {
private:
stack<int> trueStk;
stack<int> minStk;
public:
void push(int x) {
trueStk.push(x);
if (minStk.size() == )
minStk.push(x);
else
{
int tmp = getMin();
if (x <= tmp)
minStk.push(x);
}
} void pop() {
int tmp = top();
trueStk.pop();
if (tmp == getMin())
minStk.pop();
} int top() {
return trueStk.top();
} int getMin() {
return minStk.top();
}
};

【原创】leetCodeOj --- Min Stack 解题报告的更多相关文章

  1. LeetCode: Min Stack 解题报告

    Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrievi ...

  2. 【LeetCode】Min Stack 解题报告

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

  3. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  4. 【原创】leetCodeOj --- Dungeon Game 解题报告

    原题地址: https://oj.leetcode.com/problems/dungeon-game/ 题目内容: The demons had captured the princess (P) ...

  5. 【原创】leetCodeOj --- Largest Number 解题报告

    原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...

  6. 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)

    题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...

  7. 【原创】leetCodeOj --- Sort List 解题报告

    今日leetcode链表题全制霸 原题地址: https://oj.leetcode.com/problems/sort-list/ 题目内容: Sort List Sort a linked lis ...

  8. 【原创】leetCodeOj ---Partition List 解题报告

    原题地址: https://oj.leetcode.com/problems/partition-list/ 题目内容: Given a linked list and a value x, part ...

  9. 【原创】leetCodeOj --- Interleaving String 解题报告

    题目地址: https://oj.leetcode.com/problems/interleaving-string/ 题目内容: Given s1, s2, s3, find whether s3  ...

随机推荐

  1. 系统变量file.encoding对Java的运行影响有多大?(转)good

    这个话题来自: Nutz的issue 361 在考虑这个issue时, 我一直倾向于使用系统变量file.encoding来改变JVM的默认编码. 今天,我想到, 这个系统变量,对JVM的影响到底有多 ...

  2. 使用PageHeap.EXE或GFlags.EXE检查内存越界错误

    必先利其器之一:使用PageHeap.EXE或GFlags.EXE检查内存越界错误 Article last modified on 2002-6-3 ------------------------ ...

  3. rcp(插件开发)org.eclipse.ui.decorators 使用

    org.eclipse.ui.decorators这个扩展点可以为对应的节点添加不同的图标显示. 使用方式都差不多,以下就转载一下使用方式: 1.添加扩展点 org.eclipse.ui.decora ...

  4. (2)入门指南——(7)添加jquery代码(Adding our jQuery code)

    Our custom code will go in the second, currently empty, JavaScript file which we included from the H ...

  5. 模式识别 - 处理多个演示样本研究(MIL)特点(matlab)

    处理多个演示样本研究(MIL)特点(matlab) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27206325 多演示样例学习 ...

  6. Android实现位图剪切

    我们不能总是依赖于BitmapFactory 以下告诉大家怎么从Bitmaqp中截取某一部分创建新的Bitmap  系统会有一个默认png图片:icon.png 可是这个图片中最外层会有白色的 比較讨 ...

  7. 【Nginx】启动过程

    从应用程序的启动过程中main功能开始跟踪. 解析命令行參数并保存到ngx_cycle_t结构体中,在ngx_process_options函数中将保存配置文件路径. 调用ngx_add_inheri ...

  8. 10、ERP设计之系统基础管理(BS)- 平台化设计

    ShareERP 2013-09-03 ERP业务平台化是每个软件提供商必须要进行的趋势,传统定制化路线已死,不能走定制化的老路了.以往最大问的题是不能累积和沉淀技术及提升项目业务管理能力,其次是管理 ...

  9. 全然符合package.json在CommonJS中的规范

    众所周知,package.json是CommonJS规定的用来描写叙述包的文件,全然符合规范的package.json文件应该含有一下字段. name:包的名称,必须是唯一的.由小写英文字母.数字和下 ...

  10. SE 2014年5月8日

    两企业接入到 Internet(A公司和B公司),企业内部的用户及服务器均能够访问到 Internet. 2. A公司规模较大,采用了接入层/汇聚层/核心层的划分模式,接入层划分了多vLan(如图), ...