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/ 题目描述: ...
随机推荐
- java - 线程1打印1-10,当线程打印到5后,线程2打印“hello”,然后线程1继续打印
public class T { private static int a =1;//1代表线程1 2线程2 public static void main(String[] args) { fina ...
- node-session
session cookie 虽然很方便,但是使用 cookie 有一个很大的弊端,cookie 中的所有数据在客户端就可以被修改,数据非常容易被伪造,那么一些重要的数据就不能存放在 cookie 中 ...
- Django Rest Framework用户访问频率限制
一. REST framework的请求生命周期 基于rest-framework的请求处理,与常规的url配置不同,通常一个django的url请求对应一个视图函数,在使用rest-framewor ...
- 禅道BUG管理工具使用链接存储
http://www.zentao.net/book/zentaopmshelp/259.html
- 【转载】python 特殊函数 dunder function
python的特殊方法:另外一种称谓是 dunder function, 就是 under-under function的简写,就是指那些前后都带双下划线的函数. 转自这里: https://blog ...
- laravel中新增路由文件
随着业务的发展,前后台和不同平台的代码都写在一个路由文件里面会非常庞杂,这时候就诞生了拆分路由文件的需求,好在Lavravel给我们提供了支持: 1.在routes文件夹中添加新的路由文件如:admi ...
- Vue.js—组件快速入门及Vue路由实例应用
上次我们学习了Vue.js的基础,并且通过综合的小实例进一步的熟悉了Vue.js的基础应用.今天我们就继续讲讲Vue.js的组件,更加深入的了解Vue,js的使用.首先我们先了解一下什么是Vue.js ...
- CentOS7配置redis主从复制
规划 ip port role 192.168.1.31 6379 master 192.168.1.32 6379 slave 192.168.1.33 6379 slave 0.关闭防火墙 sys ...
- idea中使用FindBugs-IDEA插件
下载 - 安装 - 重启idea即可: 项目右键或者文件右键即可看到 FindBugs 选项. 选择某个选项直接检测即可.检测结果如下图: 这里的Correctness是重点关注对象.这里面的错误往 ...
- Bzoj1101 Zap(莫比乌斯反演)
题面 Bzoj 题解 先化式子 $$ \sum_{x=1}^a\sum_{y=1}^b\mathbf f[gcd(x,y)==d] \\ = \sum_{x=1}^a\sum_{y=1}^b\sum_ ...