【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 ...
随机推荐
- 认识Influxdb时序数据库及Influxdb基础命令操作
认识Influxdb时序数据库及Influxdb基础命令操作 一.什么是Influxdb,什么又是时序数据库 Influxdb是一个用于存储时间序列,事件和指标的开源数据库,由Go语言编写而成,无需外 ...
- R包开发过程记录
目的 走一遍R包开发过程,并发布到Github上使用. 步骤 1. 创建R包框架 Rsutdio --> File--> New Project--> New Directory - ...
- 【R读取报错】解决: Can't bind data because some arguments have the same name
最近读取一个数据时,报如标题的错误. args[1] <- "RT_10-VS-RT_0" all <- read.delim(paste0(args[1]," ...
- Python三元表达式,列表推导式,字典生成式
目录 1. 三元表达式 2. 列表推导式 3. 字典生成式 3.1 字典生成式 3.2 zip()方法 1. 三元表达式 """ 条件成立时的返回值 if 条件 else ...
- SQL-Union、Union ALL合并两个或多个 SELECT 语句的结果集
UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SELECT 语句中的列 ...
- window10快捷键 + 浏览器常用通用快捷键
一.window10快捷键 1.win+tab 缩小版的显示出桌面打开的所有窗口,然后再结合上下左右键加enter选择想要的窗口: 如果不想选择或者保留原有显示窗口,再按win+tab 或者 ...
- 字符scanf 的输入注意
1.注意scanf 不能有空格,如果有空格会将空格给输入进去 scanf("d "):---有空格 和scanf("d");--没有空格 有很大的区别
- 33、搜索旋转排序数组 | 算法(leetode,附思维导图 + 全部解法)300题
零 标题:算法(leetode,附思维导图 + 全部解法)300题之(33)搜索旋转排序数组 一 题目描述! 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: // 方案1 & ...
- 日常Java 2021/10/19
Java集合框架 Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射. Collection接口又有3种子类型,Li ...
- stm32串行设备接口SPI控制max31865
本人是刚入行的嵌入式,之前也没有多少项目经验,故在公司的这几个月里,可谓是如履薄冰,对于公司不同项目使用的不同的设备之多,数据手册之繁杂,让我不禁望洋兴叹,故而不愿意放弃周末这大好的自我提升时间,努力 ...