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. linux桌面的安装

    在CentOS 7中提供了两种桌面"GNOME DESKTOP" 和 "KDE Plasa Workspaces",我们以安装"GNOME DESKT ...

  2. HDU 3033 分组背包变形(每种至少一个)

    I love sneakers! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. 计算机网络(9)-----TCP可靠传输的实现

    TCP可靠传输的实现 以字节为单位的滑动窗口 滑动窗口的滑动是以字节为单位的,发送方A和接收方B在TCP三次握手的前两次握手时协商好了发送窗口和接受窗口的大小,发送方A根据B发送来的确认连接报文中标明 ...

  4. spring IOC装配Bean(注解方式)

    1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...

  5. win7 64位4GB内存下 tomcat7扩大内存

    新部署的scm项目运行第二天报: Could not retrieve transation read-only status server 无法获取事务只读状态服务器 登录界面输入正确账号密码登录后 ...

  6. java发送http的get、post请求[转]

    原文链接:http://www.cnblogs.com/zhuawang/archive/2012/12/08/2809380.html package wzh.Http; import java.i ...

  7. 基于JSON的级联列表实现

    html代码: <select id="provice" onChange="setCity()"> <option value=" ...

  8. ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository

    salve复制线程停止,尝试start slave 时报ERROR 1872错误mysql> system perror 1872 MySQL error code 1872 (ER_SLAVE ...

  9. @font-face 的用法

    现在很多设计用的字体都是五花八门的.我们切图又不能很好的让搜索爬虫搜索.就会使用@font-face方法: @Font-face目前浏览器的兼容性: Webkit/Safari(3.2+):TrueT ...

  10. yum只下载不安装:yumdownloader

    如果只想通过yum下载软件的安装包,但是不需要进行安装的话,可以使用 yumdownloader 命令. yumdownloader 命令在软件包 yum-utils 里面. 先安装 yum-util ...