[LeetCode] Min Stack 栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- getMin() -- Retrieve the minimum element in the stack.
这个是通过两个stack 维护,一个辅助栈support 维护非升序的情况,注意需要非升序,一个是用于全部data 维护。
#include <iostream>
#include <stack>
using namespace std; class MinStack {
public:
stack<int> data;
stack<int> support; void push(int x) {
if(data.empty()){
data.push(x);
support.push(x);
}
else{
data.push(x);
if(x<=support.top()) support.push(x);
}
} void pop() {
if(data.top()==support.top()) support.pop();
data.pop();
} int top() {
return data.top();
} int getMin() {
return support.top();
}
};
int main()
{
MinStack myMinStack;
myMinStack.push();
cout<<myMinStack.getMin()<<endl;
myMinStack.push();
cout<<myMinStack.getMin()<<endl;
myMinStack.push();
cout<<myMinStack.getMin()<<endl;
myMinStack.pop();
myMinStack.pop();
cout<<myMinStack.getMin()<<endl;
myMinStack.pop();
return ;
}
[LeetCode] Min Stack 栈的更多相关文章
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- LeetCode: Min Stack 解题报告
Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrievi ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode Min Stack 最小值栈
题意:实现栈的四个基本功能.要求:在get最小元素值时,复杂度O(1). 思路:链表直接实现.最快竟然还要61ms,醉了. class MinStack { public: MinStack(){ h ...
- [LeetCode] Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- [leetcode] Min Stack @ Python
原题地址:https://oj.leetcode.com/problems/min-stack/ 解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列. 代码: class MinSta ...
- LeetCode——Min Stack
Description: Design a stack that supports push, pop, top, and retrieving the minimum element in cons ...
- LeetCode() Min Stack 不知道哪里不对,留待。
class MinStack { public: MinStack() { coll.resize(2); } void push(int x) { if(index == coll.size()-1 ...
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
随机推荐
- swpan&expect交互脚本
#!/usr/bin/expectset timeout 30set user USERNAMEset pass PASSWORDspawn sudo pg_dump npi -U admin -p ...
- 用express框架实现反向代理
目前很多公司开发都是前后台分离开发,于是我用node起了一个服务,用node中的express框架实现了反向代理.(通俗易懂的讲就是我在我的电脑访问不到后台同事的电脑接口,这样做以后就可以在我本地访问 ...
- 经典dfs(depth-first search)
DFS主要在于参数的改变; 样例输入: n=4 //给定n个数字 a={1,2,4,7} //输入n个数据 k=15 //目标数字 样例输 ...
- A1058 A+B in Hogwarts (20)(20 分)
A1058 A+B in Hogwarts (20)(20 分) If you are a fan of Harry Potter, you would know the world of magic ...
- CentOS6.5生产环境系统安装
CentOS 6.5系统安装 1-1 将预先准备的CentOS 6.5安装光盘插入光驱中,开机/重启系统时,系统会进行自检,自检完毕就会出现安装系统时的引导界面,如图1-1所示.1-2 使用键盘方向键 ...
- 网络流 EK算法模板。
这篇博客讲得很好 #include<queue> #include<stdio.h> #include<string.h> using namespace std; ...
- 《鸟哥的Linux私房菜》学习笔记(5)——权限管理
一.权限的基本概念 权限:访问计算机资源或服务的访问能力. Linux中,每一个资源或者服务的权限, ...
- Linux下安装nginx,以及启动和停止
1.安装 安装nginx之前,首先确保系统已经安装了依赖:g++.gcc.openssl-devel.pcre-devel和zlib-devel软件 yum install gcc-c++ yum - ...
- Codeforces 664D Graph Coloring 二分图染色
题意: 一个无向图的每条边为红色或蓝色,有这样一种操作:每次选一个点,使与其相邻的所有边的颜色翻转. 求解是否可以经过一系列操作使所有的边颜色相同,并输出最少操作次数和相应的点. 分析: 每个点要么选 ...
- cf975d Ghosts
ref #include <algorithm> #include <iostream> #include <cstdio> #include <map> ...