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. 4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍

    4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍 天涯明月笙 关注 2018.02.20 19:23* 字数 762 阅读 135 ...

  2. thinkphp 3.2.3 - Think.class.php 解析

    class Think { public static function start() { // 注册AUTOLOAD方法 spl_autoload_register('Think\Think::a ...

  3. 【php】如何配置自主域名腾讯企业邮箱

    腾讯企业邮配置 protocal ssl smtp port 465 host smtp.exmail.qq.com user email account passwd email passwd

  4. 深入解析AJAX的原理

    AJAX:Asynchronous JavaScript And Xml(异步的JS和XML) 同步:客户端发起请求>服务端的处理和响应>客户端重新载入页面(循环) 异步:客户端实时请求& ...

  5. JZOJ 3463. 【NOIP2013模拟联考5】军训

    3463. [NOIP2013模拟联考5]军训(training) (Standard IO) Time Limits: 2000 ms  Memory Limits: 262144 KB  Deta ...

  6. 04.VUE学习之v-text v-html

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  7. JVM垃圾回收--年轻代、年老点和持久代(转)

      关键字约定 Young generation –>新生代 Tenured / Old Generation –>老年代 Perm Area –>永久代 年轻代: 所有新生成的对象 ...

  8. 异步消息处理机制,UI更新

    UI只能在主线程中完成更新,在子线程中更新UI报错如下 Only the original thread that created a view hierarchy can touch its vie ...

  9. Diycode开源项目 NotificationActivity

    1.NotificationActivity预览以及布局详解 1.1.首先看一下通知的具体页面. 1.2.然后是布局代码==>activity_fragment.xml <LinearLa ...

  10. windows服务自动备份数据库

    最近写了几个windows服务用于自动备份与删除数据: services代码如下: public partial class Service1 : ServiceBase { public Servi ...