LintCode MinStack
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的更多相关文章
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- LintCode 12.带最小值操作的栈(两种方法实现)
题目描述 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 样例 如下操作:push(1 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
- Lintcode 157. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
- Lintcode 175. 翻转二叉树
-------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...
- Lintcode 372. O(1)时间复杂度删除链表节点
----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * ...
随机推荐
- HDFS读写数据块--${dfs.data.dir}选择策略
最近工作需要,看了HDFS读写数据块这部分.不过可能跟网上大部分帖子不一样,本文主要写了${dfs.data.dir}的选择策略,也就是block在DataNode上的放置策略.我主要是从我们工作需要 ...
- collectionview cell吸顶效果
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Hiragino Sans GB"; color: #cf8724 } ...
- Bootstrap <基础二十三>页面标题(Page Header)
页面标题(Page Header)是个不错的功能,它会在网页标题四周添加适当的间距.当一个网页中有多个标题且每个标题之间需要添加一定的间距时,页面标题这个功能就显得特别有用.如需使用页面标题(Page ...
- Bootstrap <基础二十二>超大屏幕(Jumbotron)
Bootstrap 支持的另一个特性,超大屏幕(Jumbotron).顾名思义该组件可以增加标题的大小,并为登陆页面内容添加更多的外边距(margin).使用超大屏幕(Jumbotron)的步骤如下: ...
- CSipSimple配置系统
称作配置系统未免太大了一点,不过它的配置管理这一块确实有加以设计,一方面以增加灵活性,另一方面以支持第三方扩展.通过分析源码,粗略画出如下的结构图: 一.类分析 SharedPreference 一切 ...
- shell脚本连接、读写、操作mysql数据库实例
本文介绍了如何在shell中读写mysql数据库.主要介绍了如何在shell 中连接mysql数据库,如何在shell中创建数据库,创建表,插入csv文件,读取mysql数据库,导出mysql数据库为 ...
- 打不开tomcat
org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 59; schema_reference.4: 无法读取方案文档 'http: ...
- excel 转换日期
早上一朋友问我excel中如何将类似这样“19850421”的文本日期转换为“1985-04-21”.我的第一反应就是直接设置单元格格式为日期,于是打开excel试了试结果显示“########### ...
- linux下解压war文件命令
jar -xvf project.war -->解压到当前目录下. -f 指定 JAR 文件名,通常这个参数是必须的 -v 显示过程信息
- 浅谈javascript中事件冒泡与事件捕获
事件冒泡:一个事件会顺着他的层级一直往上传,一直传到document上为止,即从盒模型上看是从内到外的过程. 例: <!DOCTYPE html><html lang="e ...