https://leetcode.com/problems/min-stack/

  1. #include <vector>
  2. #include <queue>
  3. #include <map>
  4. #include <iostream>
  5. using namespace std;
  6. class MinStack {
  7. public:
  8. vector<int> vec;
  9. priority_queue<int,vector<int>,greater<int> > que,que2;
  10. void push(int x) {
  11. vec.push_back(x);
  12. que.push(x);
  13. }
  14.  
  15. void pop() {
  16. que2.push(top());
  17. vec.pop_back();
  18. }
  19.  
  20. int top() {
  21. return vec[vec.size()-1];
  22. }
  23.  
  24. int getMin() {
  25. while(!que2.empty() && que.top() == que2.top()){
  26. que.pop();que2.pop();
  27. }
  28. return que.top();
  29. }
  30. };

  

Leetcode 155 Min Stack 小顶堆+栈,优先队列实现 难度:0的更多相关文章

  1. 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 ...

  2. LeetCode 155 Min Stack(最小栈)

    翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...

  3. Java [Leetcode 155]Min Stack

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

  4. leetcode 155. Min Stack --------- java

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  5. [LeetCode] 155. Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  6. Leetcode 155 Min Stack

    题意:设计一个能输出栈内最小值的栈 该题设计两个栈,一个栈是正常的栈s,而另一个是存最小值的栈sm 在push时要判断sm是否为空,如果为空或者非空但是栈顶元素大于等于插入值的 需要在sm中插入x 同 ...

  7. Java for LeetCode 155 Min Stack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  8. CodeForces - 867E Buy Low Sell High (贪心 +小顶堆)

    https://vjudge.net/problem/CodeForces-867E 题意 一个物品在n天内有n种价格,每天仅能进行买入或卖出或不作为一种操作,可以同时拥有多种物品,问交易后的最大利益 ...

  9. 大顶堆与小顶堆应用---寻找前k小数

    vector<int> getLeastNumber(vector<int>& arr,int k){ vector<int> vec(k,); if(== ...

随机推荐

  1. sqlserver中对时间类型的字段转换

    获取当前日期利用 convert 来转换成我们需要的datetime格式. select CONVERT(varchar(12) , getdate(), 112 ) 20040912-------- ...

  2. Web Components是不是Web的未来

    今天 ,Web 组件已经从本质上改变了HTML.初次接触时,它看起来像一个全新的技术.Web组件最初的目的是使开发人员拥有扩展浏览器标签的能力,可以自由的进行定制组件.面对新的技术,你可能会觉得无从下 ...

  3. (转) CCTextFieldTTF输入框

    CCTextFieldTTF输入框 分类: cocos2d-x 2013-04-08 16:32 2964人阅读 评论(1) 收藏 举报 新建工程,testInput 修改HelloWorldScen ...

  4. Python多进程编程

    转自:Python多进程编程 阅读目录 1. Process 2. Lock 3. Semaphore 4. Event 5. Queue 6. Pipe 7. Pool 序. multiproces ...

  5. PHP 回调、匿名函数和闭包

    <?php class Product{ public $name; public $price; function __construct($name, $price){ $this-> ...

  6. web.xml配置详情 - 简要介绍

    <!--web.xml 元素简介--> <?xml version="1.0" encoding="UTF-8"?><web-ap ...

  7. xml资源getStringArray(R.array.xxx)方法

    在res/values/下新建menu_names.xml 代码如下: <?xml version="1.0" encoding="utf-8"?> ...

  8. VM安装mac及dmg文件转换iso

    今天心血来潮,突然看见一篇关于swift的入门教程,但是前提是有一台mac啊,于是对于屌丝,就只好装黑苹果或者是虚拟机上运行了,但是呢mac貌似听说(没用过)只能在inter上运行,屌丝的本子偏偏是A ...

  9. UART总线(异步)

    UART用一条传输线将数据一位位地顺序传送,以字符为传输单位通信中两个字符间的时间间隔多少是不固定的, 然而在同一个字符中的两个相邻位间的时间间隔是固定的 数据传送速率用波特率来表示, 指单位时间内载 ...

  10. 程序设计入门—Java语言 第五周编程题 2井字棋(5分)

    2 井字棋(5分) 题目内容: 嗯,就是视频里说的那个井字棋.视频里说了它的基本思路,现在,需要你把它全部实现出来啦. 你的程序先要读入一个整数n,范围是[3,100],这表示井字棋棋盘的边长.比如n ...