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. mysq 导入 导出

    导入 use legou_seller   set names utf8; source /Users/ldm/Desktop/ss.sql

  2. Widows2003开机取消按CTRL+ALT+DEL

    一, Widows2003开机取消按CTRL+ALT+DEL 1. 单击windows开始键→管理工具→本地安全策略(如下图) 2. 本地安全设置→本地策略→安全选项 3. 安全选项→右侧→找到这个文 ...

  3. Promise机制

    Promise的诞生与Javascript中异步编程息息相关,js中异步编程主要指的是setTimout/setInterval.DOM事件机制.ajax,通过传入回调函数实现控制反转.异步编程为js ...

  4. windows phone主题切换(换肤)

    之前项目做了个主题切换的功能,最后客户没来得及出第二套界面给放弃了,默哀中... 为了不让它就这样流产了,就放博客共享吧. 首先说明下原理:这个切换是通过重写资源字典里指定的样式,在运行的过程中加载指 ...

  5. 实例分析ELF文件静态链接

    参考文献: <ELF V1.2> <程序员的自我修养---链接.装载与库>第4章 静态链接 开发平台: [thm@tanghuimin static_link]$ uname ...

  6. Sqlserver2005附加数据库为只读的解决方法

    在sqlserver2005中附加数据库时,附加的数据库会变成只读的,只能进行查询操作. 解决方法 1 打开SqlServer Configuration Manager   开始->Micro ...

  7. C# .Net三层架构[转]

    C# .Net三层架构[转] 编写人:CC阿爸 2014-3-14 希望朋友们留下自己对三层架构的理解... 三层体系结构的概念     用户界面表示层(USL) 业务逻辑层(BLL) 数据访问层(D ...

  8. 复习URLHttpConnection方式GET,POST方式链接网络解析uri

    xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...

  9. markdown文档编写

    (这里面的符号都是英文的:回车是需要:空格 空格 回车) # markdown练习---1.引入图片(1和4只差!) ![周杰伦](http://p3.so.qhimg.com/bdr/_240_/t ...

  10. 做个伪IE浏览器?!【来自官网】

    原文:docwiki.embarcadero.com/RADStudio/Seattle/en/Building_a_VCL_Forms_Web_Browser_Application 选择File ...