class MinStack {
 public:
     void push(int x) {
         if(values.empty())
         {
             values.push_back(x);
             min_indices.push_back();
         }
         else
         {
             if( x < values[min_indices.back()] )
                 min_indices.push_back(values.size());
             values.push_back(x);
         }

     }

     void pop() {
         values.pop_back();
         if(values.size() == min_indices.back())
             min_indices.pop_back();
     }

     int top() {
         return values.back();
     }

     int getMin() {
         return values[min_indices.back()];
     }
 private:
     deque<int> values;
     deque<deque<int>::size_type> min_indices;
 };

Using the std::vector will lead to 'memory limit exceeded’, so i use the deque instead.

Min Stack (LeetCode) tweak it to avoid Memory Limit Exceeded的更多相关文章

  1. Wrong Answer,Memory Limit Exceeded

    错误原因: 1.在递归的时候,递归函数中忘记加返回return. 1.1做题时遇到一个奇葩错误,把它记到这里,看代码: 代码1:错误,用c++提交wrong answer,但是用g++提交却accep ...

  2. Min Stack [LeetCode 155]

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

  3. Min Stack leetcode

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

  4. leetCode Min Stack解决共享

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

  5. LeetCode算法题-Min Stack(Java实现)

    这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...

  6. LeetCode 155:最小栈 Min Stack

    LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...

  7. [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速

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

  8. leetcode 155. Min Stack --------- java

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

  9. Java [Leetcode 155]Min Stack

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

随机推荐

  1. c 陷阱与缺陷(一)

    1.程序在设计时,往往得出正确的结果,但是它并不是程序员自己想要的. 例如: printf("hello world!") 编译器进行编译时不会出现任何问题,但是结果: 提示竟然出 ...

  2. Android padding和margin的区别

    如: Padding 为内边框,指该控件内部内容,如文本/图片距离该控件的边距 Margin 为外边框,指该控件距离边父控件的边距 如: 当按钮分别设置以上两个属性时,得到的效果是不一样的. andr ...

  3. CSS制作水平垂直居中对齐

    作为前端攻城师,在制作Web页面时都有碰到CSS制作水平垂直居中,我想大家都有研究过或者写过,特别的其中的垂直居中,更是让人烦恼.这段时间,我收集了几种不同的方式制作垂直居中方法,但每种方法各有千秋呀 ...

  4. struts2笔记05-ServletActionContext

    1.ServletActionContext ServletActionContext, 这个类继承自ActionContext, 所以它具有ActionContext的很多功能,不过更重要的是它提供 ...

  5. [LeetCode]题解(python):152-Maximum Product Subarray

    题目来源: https://leetcode.com/problems/maximum-product-subarray/ 题意分析: 给定一个数组,这个数组所有子数组都有一个乘积,那么返回最大的乘积 ...

  6. iphone:自定义UIAlertView

    由于项目中有这样一个需求:需要在保存是弹出框选择保存的地点.选择UIAlertView来实现,但是要在UIAlertView中增加UISwitch的控件,这就需要自定义一个继承UIAlertView的 ...

  7. RabbitMQ是一个由erlang开发的基于AMQP(Advanced Message Queue )协议的开源实现。

    RabbitMQ是一个由erlang开发的基于AMQP(Advanced Message Queue )协议的开源实现. 1. 介绍 RabbitMQ是一个由erlang开发的基于AMQP(Advan ...

  8. Android中使用HTTP服务

    在Android中,除了使用java.net包下的API访问HTTP服务之外,我们还可以换一种途径去完成工作.Android SDK附带了Apache的HttpClient API.Apache Ht ...

  9. java 一致性哈希类实例 算法

    package com.hash; import java.util.Collection; import java.util.SortedMap; import java.util.TreeMap; ...

  10. Python文件或目录操作的常用函数

    ◆ os.listdir(path) Return a list containing the names of the entries in the directory given by path. ...