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 { * ...
随机推荐
- js当中的声明和初始化的顺序
if(!("a" in window)) { var a=1; } alert(a); 这里的alert出来undefined 这句话就相当于 var a; if(!(“a” in ...
- jquery tab 插件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 使用Word发表博客
使用浏览器编辑博客,会让你感到非常不方便,如果在没有网络的时候,就不能打开编辑器页面了,只能先写在word或其他编辑软件中.可以设置word使用word编辑并直接发布到博客. 文件 - 新 ...
- Storm进程通信机制
storm的worker进程之间消息传递机制图: 每个worker都有一个独立的监听进程,监听配置文件中配置过的端口列表supervisor.slots.ports,topology.receiver ...
- 简单几句话总结Unicode,UTF-8和UTF-16
概念 先说一说基本的概念,这包括什么是Unicode,什么是UTF-8,什么是UTF-16. Unicode,UTF-8,UTF-16完整的说明请参考Wiki(Unicode,UTF-8,UTF-16 ...
- 生成linux shadow文件
-salt $(< /dev/urandom ) -stdin $$cVcjk1yK$sfdBsYIEr800Mdr3PsICe0 $$oBrzawaF$WeVJjd2eyoEEmJykNtMH ...
- Win Form程序线程点点
消息循环 Win32窗体程序基于消息驱动的,程序的模型就是一个用户触发事件消息->系统分发事件消息->程序处理事件的循环过程. .NET Win Form程序对消息循环进行了封装,可以看到 ...
- PyAutoGUI-python版的autoit/AHK
简单介绍各个图形界面自动操作的python库,类似按键精灵\autoit\ahk(autohotkey)等等这些自动化工具.这类python库不是只是用来实现自动游戏之类的程序,业界也用这些库来做GU ...
- sublime text 3 快捷键大全
Sublime Text 3 快捷键精华版 Ctrl+Shift+P:打开命令面板Ctrl+P:搜索项目中的文件Ctrl+G:跳转到第几行Ctrl+W:关闭当前打开文件Ctrl+Shift+W:关闭所 ...
- 细说JAVA反射
Reflection 是 Java 程序开发语言的特征之一,它允许运行中的 Java 程序对自身进行检查,或者说“自审”,并能直接操作程序的内部属性.例如,使用它能获得 Java 类中各成员的名称并显 ...