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.

Example:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> Returns -3.
minStack.pop();
minStack.top();      --> Returns 0.
minStack.getMin();   --> Returns -2.

class MinStack {
public:
/** initialize your data structure here. */
MinStack() {
minValues.push(INT_MAX);
} void push(int x) {
if(x<=minValues.top()){
minValues.push(x);
} allValues.push(x);
} void pop() {
if(allValues.top()==minValues.top()){
minValues.pop();
} allValues.pop();
} int top() {
return allValues.top();
} int getMin() {
return minValues.top();
}
private:
stack<int> allValues;
stack<int> minValues;
}; /**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/

155. Min Stack (stack)的更多相关文章

  1. Java-JVM 栈帧(Stack Frame)

    一.概述 栈帧位置 JVM 执行 Java 程序时需要装载各种数据到内存中,不同的数据存放在不同的内存区中(逻辑上),这些数据内存区称作运行时数据区(Run-Time Data Areas). 其中 ...

  2. LeetCode 刷题笔记 155. 最小栈(Min Stack)

    tag: 栈(stack) 题目描述 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素 ...

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

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

  4. 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 ...

  5. 【LeetCode】155. Min Stack 最小栈 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...

  6. leetcode 155. Min Stack --------- java

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

  7. 【leetcode】155 - Min Stack

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

  8. Java [Leetcode 155]Min Stack

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

  9. 155. Min Stack

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

随机推荐

  1. git openssl 模块生成 https 请求的 ssl 测试证书

    1,请先确定安装了相关模块 1.1,git --version 1.2,openssl version -a 2,创建一个目录, cd 到该目录下 3,生成私钥  key 文件   openssl g ...

  2. 最全的select加锁分析(Mysql)

    引言 大家在面试中有没遇到面试官问你下面六句Sql的区别呢 select * from table where id = ? select * from table where id < ? s ...

  3. 开源项目几点心得,Java架构必会几大技术点

    关于学习架构,必须会的几点技术 1. java反射技术     2. xml文件处理     3. properties属性文件处理     4. 线程安全机制     5. annocation注解 ...

  4. css3-animate

    常用动画设置: effect easing duration  effect: <select name="effects" id="effectTypes&quo ...

  5. APP-8.1-百度语音应用

    1.百度语音登录地址 http://yuyin.baidu.com/ 2.控制台创建应用 3.生成签名 3.1Postman软件应用 APP-8.2-Postman应用 3.2Postman执行 UR ...

  6. css:调整placeholder样式

    input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color: #C5CACF; } input:-moz ...

  7. C++复习:异常

    异常处理机制专题 前言 1)异常是一种程序控制机制,与函数机制独立和互补     函数是一种以栈结构展开的上下函数衔接的程序控制系统,异常是另一种控制结构,它依附于栈结构,却可以同时设置多个异常类型作 ...

  8. Java IO中转换流的作用

    在<Java网络编程>中,有这样一段话: ”Reader和Writer最重要的子类是InputStreamReader和OutputStreamWriter类. InputStreamRe ...

  9. Module build failed: Error: No PostCSS Config found

    使用vue框架写pc页面时,我们经常会用到element-ui这个框架. 当我们把需要的东西都装在好运行项目的时候,有时会出现这样的错误: 这是因为缺少了一个配置文件,postcss.config.j ...

  10. 【剑指offer】单链表尾部插入一个节点

    #include <iostream> using namespace std; //链表结构体 struct ListNode { int m_Value; ListNode *next ...