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. kafka概念

    一.结构与概念解释 1.基础概念 topics: kafka通过topics维护各类信息. producer:发布消息到Kafka topic的进程. consumer:订阅kafka topic进程 ...

  2. Windows Phone零距离开发(Composite Thread组合线程)

    简洁流畅,快速响应是Windows Phone的特点也是他的买点,我们在开发App时候,也要在手机有限的硬件性能上最大限度做到UI快速响应,微软在优化手机快速响应这块做了很多底层优化工作,其中有一个就 ...

  3. iOS中UIKit——UIFont得到iOS设备上的系统字体

    for (NSString *fontFamily  in [UIFont familyNames]) { NSLog(@"字体家族是:%@",fontFamily); for(N ...

  4. Case When Exists SQL

    The Case-When-Exists expression in Oracle is really handy. Here's an example of how to use it in a s ...

  5. 利用RecyclerView CardView实现新闻卡片样式

    引入的包: demo结构: 测试代码: News.java: package com.zzw.testcardview; import java.io.Serializable; public cla ...

  6. 【转】javascript性能优化-repaint和reflow

    repaint(重绘) ,repaint发生更改时,元素的外观被改变,且在没有改变布局的情况下发生,如改变outline,visibility,background color,不会影响到dom结构渲 ...

  7. [terry笔记]更改oracle用户名

    更改oracle的用户名 之前有个需求,整理一个schema的表.索引等规划到一个表空间里,利用expdp/impdp然后remap就完成了,但是整理好的用户名remap变更了,应用又不想修改其连接信 ...

  8. m3u8

    audo apt-get install pkg-configsudo apt-get install automake autoconf m4 libtool sudo apt-get instal ...

  9. sql server 判断是否存在数据库,表,列,视图

    1 判断数据库是否存在if exists (select * from sys.databases where name = '数据库名')    drop database [数据库名] 2 判断表 ...

  10. VIM编辑器常用功能整理笔记

    vim编辑器 vi : visual Inertface 可视化接口 vim : vi improved 扩展版 语法着色 模式化编辑器: 编辑模式(命令模式): 默认模式 输入模式: 末行模式: 等 ...