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

push(1)
pop() // return 1
push(2)
push(3)
min() // return 2
push(1)
min() // return 1
 public class MinStack {
private Stack<Integer> minStack;
private Stack<Integer> stack;
public MinStack() {
// do initialize if necessary
minStack = new Stack<Integer>();
stack = new Stack<Integer>();
} public void push(int number) {
// write your code here
stack.push(number);
if (minStack.isEmpty()) {
minStack.push(number);
} else {
if(number <= minStack.peek()) {
minStack.push(number);
}
}
} public int pop() {
// write your code here
//here you use equals which stand for two peeked values
//need to be exactly same.
//If they are both null, it also works
if (stack.peek().equals(minStack.peek())) {
minStack.pop();
}
return stack.pop();
} public int min() {
// write your code here
return minStack.peek();
}
}

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. bzoj 3196: Tyvj 1730 二逼平衡树

    #include<cstdio> #include<ctime> #include<cstdlib> #include<iostream> #defin ...

  2. SELECT INTO 和 INSERT INTO区别

    (1).SELECT * INTO 新表名 FROM 旧表名 (2).INSERT INTO 新表名(列名1,列名2) SELECT * FROM 旧表名 第一句新表名不存在会自动创建, 第二句需创建 ...

  3. 一种效率更高的for循环

    var i,array=[]; for(i=array.length;i--;) { //处理代码 } 1.for循环中使用更少的变量 2.逐步减至0,这样会更快,因为同0比较比同数组的长度比较,或同 ...

  4. session的工作原理

    asp中Session的工作原理:asp的Session是具有进程依赖性的.ASP Session状态存于IIS的进程中,也就是inetinfo.exe这个程序.所以当inetinfo.exe进程崩溃 ...

  5. app标配控制器:UITabBarController

    UITabBarController UITabBarController和UINavigationController类似可以轻松的管理多个控制器,底部有一个条,底部条tabBar的高度是49. U ...

  6. Spring(2)

    Spring中的IOC和DI容器的概述 IOC(Inversion of control):其思想是反转资源获取的方向,传统的资源查找方式要求组件向容器发出请求查找资源作为回应,容器适时的返回资源 而 ...

  7. ppp数据帧的格式

    参考http://blog.chinaunix.net/uid-11639156-id-2379044.html

  8. gdb调试报错记录

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

  9. 一些pc端web事件移动端不再可行

    1.onkeyUp,onkeyDown,onkeyPress等事件不再管用,要用oninput代替   2.onclick事件会有延迟,因为手机需要等待判断是否是双击事件(ondblclick).所以 ...

  10. ios 开发中 动态库 与静态库的区别

    使用静态库的好处 1,模块化,分工合作 2,避免少量改动经常导致大量的重复编译连接 3,也可以重用,注意不是共享使用 动态库使用有如下好处: 1使用动态库,可以将最终可执行文件体积缩小 2使用动态库, ...