题目地址:https://leetcode-cn.com/problems/max-stack/

题目描述

Design a max stack that supports push, pop, top, peekMax and popMax.

  1. push(x) – Push element x onto stack.
  2. pop() – Remove the element on top of the stack and return it.
  3. top() – Get the element on the top.
  4. peekMax() – Retrieve the maximum element in the stack.
  5. 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:

  1. MaxStack stack = new MaxStack();
  2. stack.push(5);
  3. stack.push(1);
  4. stack.push(5);
  5. stack.top(); -> 5
  6. stack.popMax(); -> 5
  7. stack.top(); -> 1
  8. stack.peekMax(); -> 5
  9. stack.pop(); -> 1
  10. stack.top(); -> 5

Note:

  1. -1e7 <= x <= 1e7
  2. Number of operations won’t exceed 10000.
  3. The last four operations won’t be called when stack is empty.

题目大意

设计一个最大栈,支持 push、pop、top、peekMax 和 popMax 操作。

解题方法

双栈

这个题最大的难点在与popMax操作,即要求可以弹出栈中的最大值。

我们使用双栈,一个存放所有的数值,一个存放到当前数值为止的最大值。当要弹出最大值的时候,需要一个辅助的栈,存放数值栈中不是最大值的那些数字,弹出最大值之后,再把辅助栈中的所有元素Push到栈中。

C++代码如下:

  1. class MaxStack {
  2. public:
  3. /** initialize your data structure here. */
  4. MaxStack() {
  5. }
  6. void push(int x) {
  7. if (!maxVals.empty() && x < maxVals.top()) {
  8. maxVals.push(maxVals.top());
  9. } else {
  10. maxVals.push(x);
  11. }
  12. values.push(x);
  13. }
  14. int pop() {
  15. int val = values.top();
  16. values.pop();
  17. maxVals.pop();
  18. return val;
  19. }
  20. int top() {
  21. return values.top();
  22. }
  23. int peekMax() {
  24. int maxv = maxVals.top();
  25. return maxv;
  26. }
  27. int popMax() {
  28. int maxv = maxVals.top();
  29. stack<int> temp;
  30. while (!values.empty() && values.top() != maxv) {
  31. temp.push(values.top());
  32. values.pop();
  33. maxVals.pop();
  34. }
  35. values.pop();
  36. maxVals.pop();
  37. while (!temp.empty()) {
  38. push(temp.top());
  39. temp.pop();
  40. }
  41. return maxv;
  42. }
  43. private:
  44. stack<int> values;
  45. stack<int> maxVals;
  46. };
  47. /**
  48. * Your MaxStack object will be instantiated and called as such:
  49. * MaxStack* obj = new MaxStack();
  50. * obj->push(x);
  51. * int param_2 = obj->pop();
  52. * int param_3 = obj->top();
  53. * int param_4 = obj->peekMax();
  54. * int param_5 = obj->popMax();
  55. */

日期

2019 年 9 月 19 日 —— 举杯邀明月,对影成三人

【LeetCode】716. Max Stack 解题报告(C++)的更多相关文章

  1. [leetcode]716. Max Stack 最大栈

    Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto ...

  2. 【LeetCode】Min Stack 解题报告

    [题目] Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...

  3. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  4. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  5. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  6. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  7. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  8. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  9. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

随机推荐

  1. 认识Influxdb时序数据库及Influxdb基础命令操作

    认识Influxdb时序数据库及Influxdb基础命令操作 一.什么是Influxdb,什么又是时序数据库 Influxdb是一个用于存储时间序列,事件和指标的开源数据库,由Go语言编写而成,无需外 ...

  2. R包开发过程记录

    目的 走一遍R包开发过程,并发布到Github上使用. 步骤 1. 创建R包框架 Rsutdio --> File--> New Project--> New Directory - ...

  3. 【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]," ...

  4. Python三元表达式,列表推导式,字典生成式

    目录 1. 三元表达式 2. 列表推导式 3. 字典生成式 3.1 字典生成式 3.2 zip()方法 1. 三元表达式 """ 条件成立时的返回值 if 条件 else ...

  5. SQL-Union、Union ALL合并两个或多个 SELECT 语句的结果集

    UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SELECT 语句中的列 ...

  6. window10快捷键 + 浏览器常用通用快捷键

    一.window10快捷键  1.win+tab   缩小版的显示出桌面打开的所有窗口,然后再结合上下左右键加enter选择想要的窗口:  如果不想选择或者保留原有显示窗口,再按win+tab  或者 ...

  7. 字符scanf 的输入注意

    1.注意scanf 不能有空格,如果有空格会将空格给输入进去 scanf("d "):---有空格 和scanf("d");--没有空格 有很大的区别

  8. 33、搜索旋转排序数组 | 算法(leetode,附思维导图 + 全部解法)300题

    零 标题:算法(leetode,附思维导图 + 全部解法)300题之(33)搜索旋转排序数组 一 题目描述! 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: // 方案1 & ...

  9. 日常Java 2021/10/19

    Java集合框架 Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射. Collection接口又有3种子类型,Li ...

  10. stm32串行设备接口SPI控制max31865

    本人是刚入行的嵌入式,之前也没有多少项目经验,故在公司的这几个月里,可谓是如履薄冰,对于公司不同项目使用的不同的设备之多,数据手册之繁杂,让我不禁望洋兴叹,故而不愿意放弃周末这大好的自我提升时间,努力 ...