Min Stack

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.
 class MinStack {
List<Integer> stack = new ArrayList<Integer>();
int top = -1;
int min = Integer.MAX_VALUE; public void push(int x) {
top++;
stack.add(x);
if(min > x){
min = x; } } public void pop() { int element = stack.remove(top--);
if(element == min)
{
updateMin(); }
} public int top() {
int result = stack.get(top); return result;
} public int getMin() { return this.min;
}
public void updateMin(){
if(top == -1)
{
min = Integer.MAX_VALUE;
return;
}
min = stack.get(0);
for(int i = 1; i <= top; i++){
if(min > stack.get(i))
min = stack.get(i);
}
}
}

Min Stack的更多相关文章

  1. [LintCode] Min Stack 最小栈

    Implement a stack with min() function, which will return the smallest number in the stack. It should ...

  2. [CareerCup] 3.2 Min Stack 最小栈

    3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...

  3. leetcode 155. Min Stack --------- java

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

  4. Min Stack [LeetCode 155]

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

  5. Java [Leetcode 155]Min Stack

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

  6. 155. Min Stack

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

  7. leetCode Min Stack解决共享

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

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

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

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

随机推荐

  1. HTTP长连接200万尝试及调优

    对于一个server,我们一般考虑他所能支撑的qps,但有那么一种应用, 我们需要关注的是它能支撑的连接数个数,而并非qps,当然qps也是我们需要考虑的性能点之一.这种应用常见于消息推送系统,也称为 ...

  2. rsync 安装与配置

    1.什么是rsync Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 程两个 ...

  3. Eclipse HibernateTools安装

    Hibernate Orm是个很强大的东东,可以将数据表映射成实体,EClipse安装了HibernateTools插件后可以生成pojo,配置xml等一系列自动化工作,为我们的开发减轻了很多. 下面 ...

  4. CentOS配置FTP(VSFTPD)

    一.vsftp安装篇 # 安装vsftpd yum -y install vsftpd # 启动 service vsftpd start # 开启启动 chkconfig vsftpd on 二.v ...

  5. MYSQL的存储引擎一般只要哪些?

    根据个人个人见解: MySQL的存储引擎(构成.安全.锁) Myisam:数据操作快速的一种引擎,支持全文检索.文件保存在数据库名称为目录名的 目录中,有3个文件,分别是表定义文件(.frm).数据文 ...

  6. js 月历 时间函数 月份第一天 星期的判断

    返回值为0-6,其中返回值0为星期天:如同,php中的日期函数一样判断.

  7. 【译】Spark官方文档——Spark Configuration(Spark配置)

    注重版权,尊重他人劳动 转帖注明原文地址:http://www.cnblogs.com/vincent-hv/p/3316502.html   Spark主要提供三种位置配置系统: 环境变量:用来启动 ...

  8. delphi 基础之三 文件流操作

    文件流操作 Delphi操作流文件:什么是流?流,简单来说就是建立在面向对象基础上的一种抽象的处理数据的工具.在流中,定义了一些处理数据的基本操作,如读取数据,写入数据等,程序员是对流进行所有操作的, ...

  9. Java 数组操作

    参考了网上别人的代码,在Java中对数组的比较便利的操作是 将数组转换成集合再利用集合所提供的add remove等方法进行增删,然后再转换成原数组类型 如 String[] --> 填充至 A ...

  10. 计算两条直线的交点(C#)

    PS:从其他地方看到的源码是有问题的.下面是修正后的 /// <summary> /// 计算两条直线的交点 /// </summary> /// <param name ...