用两个stack, 第一个按顺序放所有值,第二个只放当前最小值。

注意: 1. 最小值有多个则都放到两个stack里, 尤其别忘放第二个; 2. pop时若两个stack的最上面值相等则都pop, 不等则只pop第一个stack, 但是都得返回第一个stack的pop值; 3. min时只返回第二个stack的peek值。

public class MinStack {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>(); public MinStack() {
// do initialize if necessary
} public void push(int number) {
stack1.push(number);
if(stack2.empty()){
stack2.push(number);
} else{
if(stack2.peek() >= number){
stack2.push(number);
}
}
// write your code here
} public int pop() {
if(stack1.empty() || stack2.empty()){
return -1;
} if(stack1.peek().equals(stack2.peek())){
stack2.pop();
return stack1.pop();
} else{
return stack1.pop();
}
// write your code here
} public int min() {
return stack2.peek();// write your code here
}
}

LintCode 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. Min Stack

    Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...

  6. Java [Leetcode 155]Min Stack

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

  7. 155. Min Stack

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

  8. leetCode Min Stack解决共享

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

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

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

随机推荐

  1. jquery选择相同ID

    jQuery中$("#id")只能选择第一个对象,不能选择所有相同id的元素.   通过 $("input[id='xxxx']"); 可以选择多个相同id的元 ...

  2. Pythonn 内置函数

    abs 绝对值 n = abs(-1) print(n) ========================= /usr/bin/python3.5 /home/liangml/pythonscript ...

  3. NOIP2016之反面教材提供

    NOIP 2016信息竞赛总结 竞赛历程总结: 算下来一共学了11个月的信息竞赛,从最初进来的时候大概会一点最最基础的语法,上课什么也听不懂,然后一直追进度,我想在这个阶段中我的问题主要是自己知道自己 ...

  4. 使用AjaxPro实现无刷新更新数据

    需求 在一个页面动态无刷新的更新后台得到的数据.要想无刷新的更新数据,需要使用Javascript能够获取后台返回的数据,然后通过第三方Javascript库(JQuery等)动态更新web页面DOM ...

  5. LLBLGen Pro v4.2_Patch+Keygen

    将dll文件覆盖安装目录下的文件,之后用算号器算出license文件,将license文件放在安装目录下即可. 算号器是在http://www.dxper.net/thread-408-1-1.htm ...

  6. JAVA多线程超时加载当网页图片

    先上图: 这一次没有采取正则匹配,而采取了最简单的java分割和替代方法进行筛选图片 它能够筛选如下的图片并保存到指定的文件夹 如: “http://xxxx/xxxx/xxx.jpg” 'http: ...

  7. gdb调试报错记录

    警告信息: incompatible implicit declaration of built-in function ‘strlen’ [enabled by default] 原因:未添加< ...

  8. DotSpatial 删除图层要素

    //添加图层后,定义图层,并获取图层 //遍历要素,并进行删除 FeatureSet fs = null; fs = (FeatureSet) map1.Layers[0].DataSet; //要素 ...

  9. asp.net DataTable 修改列值

    /// <summary> /// 修改数据表DataTable某一列的类型和记录值(正确步骤:1.克隆表结构,2.修改列类型,3.修改记录值,4.返回结果) /// </summa ...

  10. nginx+tomcat+dubbo单机部署多台dubbo应用

    前面的博客已经介绍如何使用nginx+tomcat,今天做的是如何在单台服务器上如何部署多台dubbo 应用的集群. 由于在项目中遇到了这个问题,今天就把它记录下来. 1.