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.
Hide Tags

Stack Data Structure

 

  这个是通过两个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 栈的更多相关文章

  1. leetCode Min Stack解决共享

    原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...

  2. LeetCode: Min Stack 解题报告

    Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrievi ...

  3. [LeetCode] Min Stack 最小栈

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

  4. LeetCode Min Stack 最小值栈

    题意:实现栈的四个基本功能.要求:在get最小元素值时,复杂度O(1). 思路:链表直接实现.最快竟然还要61ms,醉了. class MinStack { public: MinStack(){ h ...

  5. [LeetCode] Min Stack

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

  6. [leetcode] Min Stack @ Python

    原题地址:https://oj.leetcode.com/problems/min-stack/ 解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列. 代码: class MinSta ...

  7. LeetCode——Min Stack

    Description: Design a stack that supports push, pop, top, and retrieving the minimum element in cons ...

  8. LeetCode() Min Stack 不知道哪里不对,留待。

    class MinStack { public: MinStack() { coll.resize(2); } void push(int x) { if(index == coll.size()-1 ...

  9. Min Stack [LeetCode 155]

    1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...

随机推荐

  1. GoF23种设计模式之结构型模式之适配器模式

    一.概述         将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 二.适用性 1.你想使用一个已经存在的类,但是它的接口不符合 ...

  2. RNN教程之-2 LSTM实战

    前言 说出来你们不敢相信,刚才码了半天的字,一个侧滑妈的全没了,都怪这Mac的触摸板太敏感沃日.好吧,不浪费时间了,前言一般都是废话,这个教程要解决的是一个LSTM的实战问题,很多人问我RNN是啥,有 ...

  3. luogu2023 [AHOI2009]维护序列

    线段树加乘懒标记裸题. #include <iostream> #include <cstdio> using namespace std; typedef long long ...

  4. ogre3D学习基础16 -- 手动创建实体(ManualObject)

    这一节练习一下手动创建实体,用到了对象(ManualObject) 第一,依然是模板 #include "ExampleApplication.h" class Example1 ...

  5. 理解机器为什么可以学习(五)---Noise and Error

    之前我们讨论了VC Dimension,最终得到结论,如果我们的hypetheset的VC Dimension是有限的,并且有足够的资料,演算法能够找到一个hypethesis,它的Ein很低的话,那 ...

  6. Java学习5之接口

    接口不是类,而是一个特殊的名称,使用interface关键字.子类可以实现多个接口. 接口实现: public class Child extends Parent implements Interf ...

  7. docker exec小脚本

    经常要使用docker exec -it containerID bash 进入docker内部进行一些操作,干脆把它写成shell脚本节省时间. # 查看需要操作的容器id $ docker ps ...

  8. easyui在datagrid只想选择一条

    <table class="" id="jgrid" data-options="fitColumns:true,rownumbers: tru ...

  9. MessageBox:弹出窗口

    Ext.onReady(function () { Ext.MessageBox.alert("提示信息!","Hello World!"); }); Ext, ...

  10. hdu6136[模拟+优先队列] 2017多校8

    有点麻烦.. /*hdu6136[模拟+优先队列] 2017多校8*/ #include <bits/stdc++.h> using namespace std; typedef long ...