【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 ...
随机推荐
- 【R shiny】一些应用记录
目录 DT和downloadButton应用 downloadButton 中验证结果输出 添加进度条 如何确保仅在使用Shiny按下操作按钮时才触发操作 其他 DT和downloadButton应用 ...
- MariaDB——简介
一.MariaDB跟MySQL在绝大多数方面是兼容的,对于开发者来说,几乎感觉不到任何不同.是MySQL的代替品. MariaDB虽然被视为MySQL数据库的替代品,但它在扩展功能.存储引擎以及一些新 ...
- Linux搭建rsync备份服务器备份
环境: 1台rsync备份服务器,IP:10.0.0.188 1台rsync备份客户端,IP:10.0.0.51 备份数据需注意: 1. 在业务低谷时间进行备份 2. 进行备份限速 一.搭建rsync ...
- EXCEL-批量删除筛选出的行,并且保留首行
筛选->ctrl+G->可见单元格->鼠标右键->删除整行. 之前的时候,是有个方法类似于上述步骤,可以保留标题行的,但是,不知道是不是少了哪一步,上述过程总是会删除标题行.就 ...
- 关于写SpringBoot+Mybatisplus+Shiro项目的经验分享二:问题1
框架: SpringBoot+Mybatisplus+Shiro 简单介绍:关于写SpringBoot+Mybatisplus+Shiro项目的经验分享一:简单介绍 添加时,如果失败,不能正确跳转 c ...
- PC端页面转换成手机端页面的分辨率问题的理解
PC端页面转换成手机端页面的分辨率问题的理解 px vw rem 假如就以a4纸模式为设计图 ,在a3纸模式中设计,然后设计出来展示在不同的a4纸模式上 通常是 750px -> 100vw / ...
- 学会这几步,简单集成视频编辑原子能力SDK
华为视频编辑服务6.2.0版本上线后,我们为大家带来了两大变化:分别是丰富多样的AI能力和灵活选择的集成方式.为让开发者更快上手使用,今天小编带来了视频编辑原子能力SDK的具体集成方法.快来试试吧! ...
- 谈谈你对volatile的理解
1.volatile是Java虚拟机提供的轻量级的同步机制 1.1保证可见性1.2不保证原子性1.3禁止指令重排 JMM(Java内存模型Java Memory Model,简称JMM)本身是一种抽象 ...
- Spring支持5种类型的增强
Spring支持5种类型的增强:1.前置增强:org.springframework.aop.BeforeAdvice代表前置增强,因为Spring只支持方法级的增强,所以MethodBeforeAd ...
- 【Java 8】Stream.distinct() 列表去重示例
在这篇文章里,我们将提供Java8 Stream distinct()示例. distinct()返回由该流的不同元素组成的流.distinct()是Stream接口的方法. distinct()使用 ...