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(== ...
随机推荐
- html,移动端代码
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale= ...
- [python]使用virtualenv处理python版本问题
1. 更新virutalenv $ sudo easy_install --upgrade virtualenv 2. 新建virtualenv实例, 确保在your home directory ...
- iOS禁用第三方键盘
- (BOOL)application:(UIApplication *)application shouldAllowExtensionPointIdentifier:(NSString *)ext ...
- logback.xml日志配置
日志先行,日志是程序员的眼睛 控制台输出 <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAp ...
- js中cookie的使用
js中并没有封装好的存储cookie,取得cookie和删除cookie的函数,所以必须得自己手动处理,并且cookie中也只能存储字符串,不能存储数组等复杂的数据类型. // 添加cookie fu ...
- 用一个案列详细讲解UITextFiled
一. 登陆界面的搭建 首先涉及到登录界面状态栏颜色的问题,我们需要将状态栏颜色改为白色,可以在控制器内实现方法更改 - (UIStatusBarStyle)preferredStatusBarStyl ...
- 合并基因表达水平(merge gene expression levels, FPKM)
使用tophat和cufflinks计算RNA-seq数据的表达水平时,当一个基因在一个样本中有多个表达水平时需要合并它们的表达水平. This code is a solution to colla ...
- Java开发中经典的小实例-(用*打印图案)
public class Test19 { public static void main(String[] args) { // TODO Auto-generated meth ...
- 第一课 python的几种环境配置
第一种,pythom+eclipse+pydev 这种安装方式比较简单,网上教程比较多,需要注意的是安装eclipse前需要安装jdk.具体过程不再啰嗦了.下面主要讲讲在64位系统下安装numpy,s ...
- oracle 之 函数
本次主题 青涩/色 函数的结束一定要使用return语句返回一个与声明匹配的值 --语法: create[or replace] function<函数名> [(参数列表)] return ...