leetCode(45):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.
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的更多相关文章
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- 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 ...
- LeetCode 155 Min Stack(最小栈)
翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- 【LeetCode】Min Stack 解题报告
[题目] Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...
- 【leetcode】Min Stack -- python版
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- Java for LeetCode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode之Min Stack 实现最小栈
LeetCode相关的网上资源比较多,看到题目一定要自己做一遍,然后去学习参考其他的解法. 链接: https://oj.leetcode.com/problems/min-stack/ 题目描述: ...
随机推荐
- C语言实现二叉排序树
程序以'#'结尾的二叉排序树. /*(双重指针 BSTree *T)问:数据结构中 二叉树建立结点为什么用 双重指针?详细解释下双重指针 答:指针的指针.因为树的结点要用指针描述.如果只用指针,作形参 ...
- spring-web涉及jar包说明
<!-- spring-context, spring-aop, spring-beans, spring-core, spring-expression --> <dependen ...
- UVALive - 7042 The Problem to Make You Happy 博弈
题目大意:给你一个有向图, Bob 和 Alice 在做游戏,每轮他们走一步,当Bob 和 Alice在同一个点或者 Bob无路可走,Bob输,否则Alice输. 思路:因为在Bob赢的时候存在有环的 ...
- Rsync+Inotify 搭建实时同步数据
1.安装软件包 # yum install inotify-tools # yum -y install rsync 2.同步机器相互添加信任 [root@host-10-0-100-106 ~]# ...
- ASOP编译说明
具体说明https://source.android.com/source/ 源码下载https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/ 1 搭建编译环境使 ...
- CentOS 7.4 下面安装 jdk 10 的一点总结
CentOS 7.4 下面安装 jdk 10 的一点总结 一.前期工作 1.检验系统原版本 [root@zck ~]# java -version java version "1.7.0_& ...
- CSS3主要的几个样式笔记
1.边框:border-color: 设置对象边框的颜色. 使用CSS3的border-radius属性,如果你设置了border的宽度是X px,那么你就可以在这个border上使用X ...
- CodeForces 767B The Queue
模拟. 情况有点多,需要仔细.另外感觉题目的$tf$有点不太对......而且数据水了. $0$ $5$ $2$ $2$ $0$ $5$ 这组数据按照题意的话答案可以是$2$和$4$,但是好多错的答案 ...
- ExtJs之列表常用CRUD
前端代码: Ext.onReady(function(){ Ext.define('Person', { extend: 'Ext.data.Model', fields: [{name: 'id', ...
- apue第16章笔记
intel 都是小端,小端即最低有效字节在最低地址上. tcp/ip协议栈使用大端字节序. connect失败可能是一瞬时的,用指数补偿算法处理,exponential backoff.但是在bsd套 ...