Min Stack leetcode
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.
使用一个栈,和一个保存最小值的变量min
栈中保存的元素是 新加数和当前min的差,然后更新min,使min保持最小
利用差值使每个栈元素可以包含两个信息量
class MinStack {
public:
void push(int x) {
if (sta.empty())
{
sta.push();
min = x;
}
else
{
sta.push(x - min);
if (x < min)
min = x;
}
}
void pop() {
if (sta.empty())
return;
long top = sta.top();
if (top < )
min = min - top;
sta.pop();
}
int top() {
if (sta.empty())
return ;
long top = sta.top();
if (top < )
return min;
else
return min + top;
}
int getMin() {
return min;
}
private:
stack<long> sta;
long min;
};
Min Stack leetcode的更多相关文章
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
- Min Stack (LeetCode) tweak it to avoid Memory Limit Exceeded
class MinStack { public: void push(int x) { if(values.empty()) { values.push_back(x); min_indices.pu ...
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
- LeetCode 155:最小栈 Min Stack
LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...
- 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: Min Stack 解题报告
Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrievi ...
随机推荐
- List与Linkedlist、Arrylist、Vector、Map应用
1.List与LinkedList List是数组链表 LinkedList是指针链表 选择List还是LinkedList要看你的使用特点. 数组链表访问快,复 ...
- 计算数据库中30天以内,30-60天,60-90天,90天以外的数据的个数(用sql实现)
30天以内:select count(*) from TB where datediff(day,字段名,getdate()) between 0 and 3030-60天:select count( ...
- 关于MATSIM中,如何关闭自动加载dtd的问题
有用过MATSIM做交通仿真的朋友应该都知道,在创建Scenario时,会默认加载matsim官网的netword的dtd文件,然后因为网络的问题,加载往往会报错,从而造成系统异常退出,如图所示: 根 ...
- ubuntu和Deepin下chrome浏览器提示flash下载失败或者过期的解决方案
问题:更新了下Deepin系统,打开chrome发现,视频放不了了,提示flash"下载失败" 谷歌浏览器版本是Version 55.0.2883.87 (64-bit) flas ...
- bootstropt-table 大量字段整体表单上传之时间处理
js 中用$('#addUserForm').serialize(),//获取表单中所有数据 传送到前台 (controller) $.ajax({ type : "POST", ...
- vue-router跳转页面
小结放在前:先祝大家新年快乐!鸡年大吉大利!在新的一年里大家都有跳跃般的成长!作为新年的第一篇文章,就拿他来给大家拜个年!!!文章前部份讲解了vue-router路由的配置,后半部分为去年的文章vue ...
- ML2 配置 OVS VxLAN - 每天5分钟玩转 OpenStack(146)
今天我们开始学习 OVS 如何实现 Neutron VxLAN,关于 VxLAN 的概念以及 Linux Bridge 实现,大家可以参考前面相关章节. Open vSwitch 支持 VXLAN 和 ...
- php下安装动态扩展库的相关事项
php下安装动态扩展库的相关事项 我下载的Apache版本为2.4,PHP版本为7.0. 将Apache与PHP集成配置好后(PHP安装目录为:G:\computer\web\php7,apache安 ...
- ajax问题
1. 代码:var i;for(i=0;i<10;i++){ ajaxServise(i);} 在for循环中调用ajax方法 补充页面上的数据,这样写是错误的,他不会每执行一次fo ...
- db_link
1.查询 SYSDBA登录, sys登录 SELECT * FROM SYS.link$; select owner,object_name from dba_objects where obj ...