【LeetCode】716. Max Stack 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/max-stack/
题目描述
Design a max stack that supports push, pop, top, peekMax and popMax.
push(x)– Push element x onto stack.pop()– Remove the element on top of the stack and return it.top()– Get the element on the top.peekMax()– Retrieve the maximum element in the stack.popMax()– Retrieve the maximum element in the stack, and remove it. If you find more than one maximum elements, only remove the top-most one.
Example 1:
MaxStack stack = new MaxStack();
stack.push(5);
stack.push(1);
stack.push(5);
stack.top(); -> 5
stack.popMax(); -> 5
stack.top(); -> 1
stack.peekMax(); -> 5
stack.pop(); -> 1
stack.top(); -> 5
Note:
-1e7 <= x <= 1e7- Number of operations won’t exceed 10000.
- The last four operations won’t be called when stack is empty.
题目大意
设计一个最大栈,支持 push、pop、top、peekMax 和 popMax 操作。
解题方法
双栈
这个题最大的难点在与popMax操作,即要求可以弹出栈中的最大值。
我们使用双栈,一个存放所有的数值,一个存放到当前数值为止的最大值。当要弹出最大值的时候,需要一个辅助的栈,存放数值栈中不是最大值的那些数字,弹出最大值之后,再把辅助栈中的所有元素Push到栈中。
C++代码如下:
class MaxStack {
public:
/** initialize your data structure here. */
MaxStack() {
}
void push(int x) {
if (!maxVals.empty() && x < maxVals.top()) {
maxVals.push(maxVals.top());
} else {
maxVals.push(x);
}
values.push(x);
}
int pop() {
int val = values.top();
values.pop();
maxVals.pop();
return val;
}
int top() {
return values.top();
}
int peekMax() {
int maxv = maxVals.top();
return maxv;
}
int popMax() {
int maxv = maxVals.top();
stack<int> temp;
while (!values.empty() && values.top() != maxv) {
temp.push(values.top());
values.pop();
maxVals.pop();
}
values.pop();
maxVals.pop();
while (!temp.empty()) {
push(temp.top());
temp.pop();
}
return maxv;
}
private:
stack<int> values;
stack<int> maxVals;
};
/**
* Your MaxStack object will be instantiated and called as such:
* MaxStack* obj = new MaxStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->peekMax();
* int param_5 = obj->popMax();
*/
日期
2019 年 9 月 19 日 —— 举杯邀明月,对影成三人
【LeetCode】716. Max Stack 解题报告(C++)的更多相关文章
- [leetcode]716. Max Stack 最大栈
Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto ...
- 【LeetCode】Min Stack 解题报告
[题目] Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
随机推荐
- yum和apt-get的用法和区别
一般来说著名的linux系统基本上分两大类: 1.RedHat系列:Redhat.Centos.Fedora等 2.Debian系列:Debian.Ubuntu等 RedHat 系列 1 常见的安装包 ...
- 中兴交换机基础配置(备份、dhcp中继、monitor)
1. 备份配置 格式: copy tftp/sftp/ftp [vrf mng] root: 本地文件 远端文件 1. 通过tftp进行备份,vrf mng表示指定使用管理口链路连接 copy tft ...
- Machine Learning读书会,面试&算法讲座,算法公开课,创业活动,算法班集锦
Machine Learning读书会,面试&算法讲座,算法公开课,创业活动,算法班集锦 近期活动: 2014年9月3日,第8次西安面试&算法讲座视频 + PPT 的下载地址:http ...
- 表格合并单元格【c#】
gridBranchInfo.DataSource = dtBranchViewList; gridBranchInfo.DataBind(); Random random = new Random( ...
- python生成器浅析
A 'generator' is a function which returns a generator iterator. It looks like a normal function exce ...
- js 如何全部替代一个子串为另一个子串
更多描述: 假设有一个字符串 `hello. hello. hello. ` 需要替换为 `AAA`,即把 `hello. ` 替换为 `A` 如果需要全量替换字符串,可以使用 String.prot ...
- 【JAVA开发】浅析双亲委派机制
双亲委派机制存在的意义 双亲委派只是一种说法,个人觉得叫单亲委派更合适,因为向上传递的父类只有一个,估计只是翻译过来的,形成的一种习惯,大家可以当做单亲委派 四种加载器都是用于类的加载,只是加载的对象 ...
- go 代理
环境变量中设置 #GO111MODULE=auto GOPROXY=https://goproxy.io 如果不第一次,则在命令行设置 go env -w GO111MODULE=on go env ...
- 【Python】【Basic】【数据类型】运算符与深浅拷贝
运算符 1.算数运算: 2.比较运算: 3.赋值运算: 4.逻辑运算: 5.成员运算: 三元运算 三元运算(三目运算),是对简单的条件语句的缩写. # 书写格式 result = 值1 if 条件 ...
- html标签设置contenteditable时,去除粘贴文本自带样式
在一个div标签里面加了可编辑的属性,从别的地方复制了一串文本,只想把文本内容存到接口里面,结果发现文本自带的标签和样式都会存进去. $(".session-new-name"). ...