Leetcode 155 Min Stack 小顶堆+栈,优先队列实现 难度:0
https://leetcode.com/problems/min-stack/
#include <vector>
#include <queue>
#include <map>
#include <iostream>
using namespace std;
class MinStack {
public:
vector<int> vec;
priority_queue<int,vector<int>,greater<int> > que,que2;
void push(int x) {
vec.push_back(x);
que.push(x);
} void pop() {
que2.push(top());
vec.pop_back();
} int top() {
return vec[vec.size()-1];
} int getMin() {
while(!que2.empty() && que.top() == que2.top()){
que.pop();que2.pop();
}
return que.top();
}
};
Leetcode 155 Min Stack 小顶堆+栈,优先队列实现 难度:0的更多相关文章
- 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 ...
- LeetCode 155 Min Stack(最小栈)
翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Leetcode 155 Min Stack
题意:设计一个能输出栈内最小值的栈 该题设计两个栈,一个栈是正常的栈s,而另一个是存最小值的栈sm 在push时要判断sm是否为空,如果为空或者非空但是栈顶元素大于等于插入值的 需要在sm中插入x 同 ...
- Java for LeetCode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- CodeForces - 867E Buy Low Sell High (贪心 +小顶堆)
https://vjudge.net/problem/CodeForces-867E 题意 一个物品在n天内有n种价格,每天仅能进行买入或卖出或不作为一种操作,可以同时拥有多种物品,问交易后的最大利益 ...
- 大顶堆与小顶堆应用---寻找前k小数
vector<int> getLeastNumber(vector<int>& arr,int k){ vector<int> vec(k,); if(== ...
随机推荐
- ligerui_ligerTree_005_动态增加“树”节点
动态添加ligerTree节点:效果图: 源码地址:http://download.csdn.net/detail/poiuy1991719/8571255 <%@ page language= ...
- swift中editingStyleForRowAtIndexPath的写法
效果图: 首先要实现这句tableView.setEditing(true, animated: true)才能弹出左侧的小圆圈 然而在oc中tableview删除的写法百度一下很常见但是swift中 ...
- asp.net关于页面不回发,不生成__doPostBack方法问题的完美解决方案
1.有可能是使用net4.0+iis6之后没有打补丁.解决办法在IE10中登录我公司的一个网站时,点击其它菜单,页面总会自动重新退出到登录页,后检查发现,IE10送出的HTTP头,和.AUTH Coo ...
- Python之反射
一.引言 有时候我们会碰到类似这样的需求,就是想要执行类的某个方法,或者需要对对象的某个参数赋值,而方法名或参数名已经包装在类中并不能去顶,需要通过参数传递字符串的形式输入.在这样的情况你会选择什么样 ...
- [已解决] windows 80端口被占用
看下是不是IIS开着,如果是关掉就OK拉 原文地址:http://www.cnblogs.com/gifisan/p/5822156.html
- TortoiseSVN安装使用
TortoiseSVN是windows平台下Subversion的免费开源客户端. 一般我们都是先讲讲服务器的配置,然后再讲客户端的使用,但是在TortoiseSVN上,却可以反过来.因为,如果你的要 ...
- [问题2014A09] 复旦高等代数 I(14级)每周一题(第十一教学周)
[问题2014A09] 设 \(A,B\) 分别是 \(3\times 2\), \(2\times 3\) 矩阵且满足\[AB=\begin{bmatrix} 8 & 2 & -2 ...
- Python:C语言扩展
1. 概述 Python 可以非常方便地和 C 进行相互的调用. 一般,我们不会使用 C 去直接编写一个 Python 的模块.通常的情景是,我们需要把 C 的相关模块包装一下,然后在 Python ...
- Winform中checklistbox控件的常用方法
Winform中checklistbox控件的常用方法最近用到checklistbox控件,在使用其过程中,收集了其相关的代码段1.添加项checkedListBox1.Items.Add(" ...
- javascript 去掉空格之后的字符 正则表达式
从后端数据库读取时间时,经常会把整个日期年月日包括时分秒都取到,如2015-1-28 14:56:00,但是一般的我们只需要前面的年月日就行了.一个简单的方法,直接用split(" &quo ...