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. SQL Server 2008 R2 找不到 Install SQL Server Profiler 找不到 事件探查器 解决

    摘自: http://blog.csdn.net/yuxuac/article/details/8992893 SQL Server 2008 R2 Express Edition - Install ...

  2. Android IOS WebRTC 音视频开发总结(二三)-- hurtc使用说明

    本文主要介绍如何测试基于浏览器和手机的视频通话程序,转载请说明出处,文章来自博客园RTC.Blacker,更多详见www.blackerteam.com   很多人想测试浏览器(包括浏览器版本和桌面e ...

  3. UTF-8 GBK GB2312 之间的区别和关系

    UTF-8:Unicode TransformationFormat-8bit,允许含BOM,但通常不含BOM.是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24为(三 ...

  4. Ubuntu系统使用记录(持续更新)

    本篇文章记录在虚拟机上跑Ubuntu16.04遇到的一系列问题,熟悉一下Ubuntu的相关操作,进入终端的方法ctrl+alt+t. 1.修改屏幕分辨率,进入系统默认的是800x600 即便能够进入s ...

  5. jQuery插件Skippr实现焦点图

    史上效果最好的焦点图幻灯片jQuery插件Skippr,轻量级插件.响应式布局插件,强大的参数自定义 配置,可自定义切换速度.切换方式.是否显示左右箭头.是否自动播放.自动播放间隔时间等配置 参数,调 ...

  6. winform中嵌入Ppt、Word、Excel

    1.下载DsoFramer_KB311765_x86.exe 2.安装,默认路径安装C:\DsoFramer. 3.注册:开始菜单——>运行 输入:regsvr32 C:\DsoFramer\d ...

  7. 《第一行代码--Android》阅读笔记之数据持久化

    1.升级数据库 为了避免手工清空数据(或卸载重装APP),重写SQLiteOpenHelper里面的onUpgrade()方法   引用自http://blog.csdn.net/longvslove ...

  8. 开篇 hello 内Cool超人

    经过一年时间看到asp.net mvc一直被受微软开发团队的注重.与之相比的silverlight我感觉到有点力不从心.除去silverlight第一次运行要安装Runtime不说,产品不可能只运行在 ...

  9. tomcat 配置文件下载目录

    tomcat可提供文件的直接下载.有两种方式. 第1种 放到ROOT 目录下 然后在网址中访问: http://ip:8080/download.zip 便可下载 第2种 希望使用自己的文件路径. 在 ...

  10. C++求最小公倍数

    题目内容:求两个正整数的最小公倍数. 输入描述:输入数据含有不多于50对的数据,每对数据由两个正整数(0<n1,n2<100000)组成. 输出描述:对于每组数据n1和n2,计算最小公倍数 ...