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 出现冲突时的解决办法

    <一> 二者兼得最麻烦 1, 出现冲突一般出现在群体开发两个及以上开发者同时修改同一个文件时 2, 具体表现为 git pull , git push 和 git commit 命令执行失 ...

  2. centos7.5安装golang

    1.下载 [root@localhost bin]#wget https://dl.google.com/go/go1.10.2.linux-amd64.tar.gz [root@localhost ...

  3. linux配置sphinx

    1. 配置索引 cd /usr/local/sphinx/etc/ cp sphinx.conf.dist sphinx.conf //备份配置文件,防止改错 vim sphinx.conf 配置文件 ...

  4. Xshell 公钥登入服务器

    1:生成公钥 此时有test.pub文件 2:linux CentOS 7 配置 2.1 test.pub 存入/root/test.pub目录下面 2.2 确保authorized_keys文件内容 ...

  5. Choose unique values for the 'webAppRootKey' context-param in your web.xml files!

    在Tomcat的server.xml中配置两个context,出现其中一个不能正常启动,交换配置顺序,另一个又不能正常启动,即始终只有第二个配置能启动的情况.如果单独部署,都没有问题.报错大致内容如下 ...

  6. Tornado的安装使用

    https://blog.csdn.net/a312024054/article/details/52207367 tornado原理: tornado的使用 import tornado.ioloo ...

  7. django 之manytomany

    https://www.cnblogs.com/changbaishan/p/8056762.html https://blog.csdn.net/hpu_yly_bj/article/details ...

  8. 13.BeanUtils组件-基础.md

    目录 用途 基本属性的设置 Map数据的拷贝 对象的拷贝 转换器 用途 可以用来对JavaBean的各种增强操作 基本属性的设置 package per.liyue.code.beanutildemo ...

  9. Hibernate 再接触 继承映射

    用一张 每一个类一张表 建立外键 第一种 一张总表 Person package com.bjsxt.hibernate; import javax.persistence.Discriminator ...

  10. 通过DOS命令批量重命名文件

    以下为提供的两种方法:遍历当前目录下的所有文件名以.avi结尾的文件,然后权限规则进行修改(规则含义请自行查找资料).第一种方法有缺陷,更改完所有的文件名后,会多改一次.请斟酌使用.第二种方法解决了第 ...