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.
class MinStack {
public:
void push(int x) {
data.push(x);//数据栈正常压入
if(minData.empty())
{//最小栈数据栈压入时要进行推断。假设被压入的数据不影响最小值,则直接再压入
//最小栈栈顶元素,否则压入x
minData.push(x);
}
else
{
if(x<minData.top())
{
minData.push(x);
}
else
{
minData.push(minData.top());
}
}
} void pop() {//弹出时,两个栈都须要弹出
data.pop();
minData.pop();
} int top() {
return data.top();
} int getMin() {
return minData.top();
} private:
stack<int> data;
stack<int> minData;
};

leetCode(45):Min Stack的更多相关文章

  1. leetcode 155. Min Stack --------- java

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

  2. Java [Leetcode 155]Min Stack

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

  3. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  4. LeetCode 155 Min Stack(最小栈)

    翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...

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

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

  6. 【LeetCode】Min Stack 解题报告

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

  7. 【leetcode】Min Stack -- python版

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

  8. Java for LeetCode 155 Min Stack

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

  9. LeetCode之Min Stack 实现最小栈

    LeetCode相关的网上资源比较多,看到题目一定要自己做一遍,然后去学习参考其他的解法. 链接: https://oj.leetcode.com/problems/min-stack/ 题目描述: ...

随机推荐

  1. ueditor在QQ浏览器或者IE浏览器中无法加载

    因为IE浏览器有兼容问题,打开网址,浏览器不一定以最新的文档模式加载.按F12查看 在你网址的head标签中加入:<meta http-equiv="x-ua-compatible&q ...

  2. PHP定界符出现错误

    Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or heredoc end (T ...

  3. hive的窗口函数1

    Hive中提供了越来越多的分析函数,用于完成负责的统计分析.抽时间将所有的分析窗口函数理一遍,将陆续发布.今天先看几个基础的,SUM.AVG.MIN.MAX.用于实现分组内所有和连续累积的统计. 1. ...

  4. python mock的简单使用

    参考文章: http://blog.csdn.net/wenph2008/article/details/46862771 内容待填充...

  5. 创建一个支持ES6的Nodejs项目

    文章来自于:https://www.codementor.io/iykyvic/writing-your-nodejs-apps-using-es6-6dh0edw2o 第一步:创建项目文件夹并初始化 ...

  6. centos 新增用户, 然后他在主目录添加网站403Forbbiden

    是 /home/zhou  这个文件夹的权限不够 往往看了网站目录并且给了777权限了,还是403,就是因为父目录的权限不够

  7. HDU 6467.简单数学题-数学题 (“字节跳动-文远知行杯”广东工业大学第十四届程序设计竞赛)

    简单数学题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  8. HDU 3485【101】 51nod 1668【010】 joj 2171【111】动态规划

    有一个只含0和1的长度为n的串,问不含有101的所有串的个数. ——不存在连续的101.010.111的字符串数量 HDU:https://cn.vjudge.net/problem/HDU-3485 ...

  9. HDU 4865 Peter's Hobby

    $dp$. 这题的本质和求一个有向无环图的最长路径长度的路径是一样的. $dp[i][j]$表示到第$i$天,湿度为$a[i]$,是第$j$种天气的最大概率.记录一下最大概率是$i-1$天哪一种天气推 ...

  10. 洛谷P3942将军令

    啦啦啦,又是五月天的歌------ 题目传送门 那么来分析下题目;给定你一棵树,告诉你一支队伍能管辖的范围,求能覆盖整棵树的最少队伍数. 嘛,如果不会做,第一个想到的肯定是暴搜嘛,但是代码打起来肯定也 ...