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

It should support push, pop and min operation all in O(1) cost.

Example

  1. push(1)
  2. pop() // return 1
  3. push(2)
  4. push(3)
  5. min() // return 2
  6. push(1)
  7. min() // return 1
  8.  
  1. public class MinStack {
  2. private Stack<Integer> minStack;
  3. private Stack<Integer> stack;
  4. public MinStack() {
  5. // do initialize if necessary
  6. minStack = new Stack<Integer>();
  7. stack = new Stack<Integer>();
  8. }
  9.  
  10. public void push(int number) {
  11. // write your code here
  12. stack.push(number);
  13. if (minStack.isEmpty()) {
  14. minStack.push(number);
  15. } else {
  16. if(number <= minStack.peek()) {
  17. minStack.push(number);
  18. }
  19. }
  20. }
  21.  
  22. public int pop() {
  23. // write your code here
  24. //here you use equals which stand for two peeked values
  25. //need to be exactly same.
  26. //If they are both null, it also works
  27. if (stack.peek().equals(minStack.peek())) {
  28. minStack.pop();
  29. }
  30. return stack.pop();
  31. }
  32.  
  33. public int min() {
  34. // write your code here
  35. return minStack.peek();
  36. }
  37. }

In this problem, we want to get the current smallest value and implemnt a stack. The main issue is to get current smallest value. So we should use another stack to remember the current smallest values and delete them when we pop that value out.

Trcks: Use stack.peek().equals(minStack.peek()) instead of stack.peek() == minStack.peek() becuase peek() method could return null value and if they are both null it is also okay.

LintCode MinStack的更多相关文章

  1. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  2. LintCode 12.带最小值操作的栈(两种方法实现)

    题目描述 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 样例 如下操作:push(1 ...

  3. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  4. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  5. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  6. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

  7. Lintcode 157. 判断字符串是否没有重复字符

    ------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...

  8. Lintcode 175. 翻转二叉树

    -------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...

  9. Lintcode 372. O(1)时间复杂度删除链表节点

    ----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * ...

随机推荐

  1. 临界区 TRTLCriticalSection 和 TCriticalSection

    临界区对象TCriticalSection(Delphi) 与 TRtlCriticalSection 的区别 TRtlCriticalSection 是一个结构体,在windows单元中定义: 是I ...

  2. Git 配置

    在 windows 上安装完 Git 会右键菜单中看到 Git 的快捷打开选项, 点 Git Bash Here 就可以在当前目录下打开 Git 的命令行 Git shell,初次使用 Git 先配置 ...

  3. MVB设备分类

    连接在MVB上的设备按性能可以分为5类 MVB上的设备应具备下面六个性能中的一个或多个. MVB设备的性能 性能 说明 分类 设备状态 设备被轮询时能够发送出其设备状态 1,2,3,4,5 过程数据 ...

  4. NodeJs使用asyncAwait两法

    async/await使用同步的方式来书写异步代码,将异步调用的难度降低到接近于0,未来必将大放异彩.然而在当下,由于标准化的缓存步伐,async/await尚在ES7的草案中.为了尝先,特试用了下面 ...

  5. Maven 排除第三方jar包所依赖的其他依赖

    单依赖过滤:可以过滤一个或者多个,如果过滤多个要写多个<exclusion>. <dependency> <groupId>org.apache.hbase< ...

  6. 杭电ACM1002

    原题:http://acm.hdu.edu.cn/showproblem.php?pid=1002 #include <stdio.h> #include <string.h> ...

  7. VS调试时下不到断点的处理方式。

    调试无法命中断点的情况我想很多人遇到过,反正我是遇到过很多次了,有时候是没有生成项目或解决方案,有时候是调试版本不一致. 当然还有其他的情况都已经忘记如何处理的了. 今天在release模式下要调试代 ...

  8. JQuery ajax 异步传一个数组到 .net后台

    可能使用JQuery Ajax传值到后台一个字符串,或者序列化后的表单大家都使用过,但是某些项目,需要我们一次传值一个数组到后台,这个时候有什么好的办法呢? 1.JS将数组转换为一个字符串,然后传值到 ...

  9. freeCAD定制界面

    由于freecad接口是基于现代Qt工具包,它非常先进.窗口,菜单,工具栏和其他工具都可以修改,移动工作台,共享,键盘快捷键都可以设置.修改,以及宏,它可以录制和播放.定制界面是访问Tools -&g ...

  10. sql语句查询最近七天 三十天 数据

    几个小时内的数据 DATE_SUB(NOW(), INTERVAL 5 HOUR) 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); ...