题目地址:

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. AIR

    There is a meaning for wings that cannot fly,it's a previous memory of when you once flew through th ...

  2. MySQL 更新走全表和索引的评估记录数

    #!/usr/bin/perl use DBI; $db_name='scan'; $ip='127.0.0.1'; $user="root"; $passwd="123 ...

  3. 如何debug ruby

    how to debug ruby: 1. 第一种方法,直接使用ruby内建的debug在命令行调试,这个个gdb或者pdb的命令差不多. ruby -r debug yourubyfile.rb 2 ...

  4. 利用jquery+iframe做一个ajax上传效果

    以下是自学it网--中级班上课笔记 网址:www.zixue.it html页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict ...

  5. javascript中使用Map

    mis.comm.js.Map = function() { this.elements = new Array(); //获取MAP元素个数 this.size = function() { ret ...

  6. Android 手势&amp;触摸事件 MotionEvent

    1.http://blog.csdn.net/omg_2012/article/details/7881443 这篇相当好啊 2.http://blog.csdn.net/android_tutor/ ...

  7. 关于索引删除的策略IndexDeletionPolicy

    关于索引删除的策略IndexDeletionPolicy . public IndexWriter(Directory d, Analyzer a, boolean create)          ...

  8. Android Fragment的介绍与使用(案例Demo)

    应用场景: 众所了解Android上的界面展示都是通过Activity实现的,可是Activity也有它的局限性,相同的界面在手机上显示可能非常好看,在平板上就未必了.为了让界面能够在平板上更好地展示 ...

  9. ruby中的模块

    什么是模块 模块(module)是Ruby特有的功能之一.类用来表现具有数据与行为(程序)的"东西", 而模块大致来说,则是只有程序部分的集合体.类与模块最大的不同在于: 1.模块 ...

  10. Linux创建修改删除用户和组

    Linux 创建修改删除用户和组 介绍 在日常的维护过程中创建用户操作用的相对会多一些,但是在这个过程中涉及到的知识点就不单单就是useradd了,接下来就来详细了解账号管理的相关信息. 用户信息 先 ...