Google/LintCode:M-带最小值操作的栈
来源:互联网 发布:大奖章基金骗局知乎 编辑:程序博客网 时间:2024/06/05 00:24
题目
题目来源:Link
实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值。
你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成。
注意事项
如果堆栈中没有数字则不能进行min方法的调用
样例
如下操作:push(1),pop(),push(2),push(3),min(), push(1),min() 返回 1,2,1
分析
维护两个栈,一个栈用于存数据,另一栈用于存对应数据 push 进来时的最小值
代码
public class MinStack { Stack<Integer> s = new Stack<Integer>(); Stack<Integer> smin = new Stack<Integer>(); int min=Integer.MAX_VALUE; public MinStack() { // do initialize if necessary } public void push(int number) { // write your code here min = Math.min(min, number); s.add(number); smin.add(min); } public int pop() { // write your code here smin.pop(); if(smin.isEmpty()){ min=Integer.MAX_VALUE; }else{ min=smin.peek(); } return s.pop(); } public int min() { // write your code here return smin.peek(); }}
阅读全文
0 0
- Google/LintCode:M-带最小值操作的栈
- LintCode 带最小值操作的栈
- lintcode-带最小值操作的栈-12
- LintCode:带最小值操作的栈
- lintcode,带最小值操作的栈
- lintcode,带最小值操作的栈
- LintCode 12 带最小值操作的栈
- lintcode -- 带最小值操作的栈
- [LintCode]12.带最小值操作的栈
- LintCode-----12.带最小值操作的栈
- lintcode带最小值操作的栈
- Lintcode 12. 带最小值操作的栈
- Lintcode 带最小值的栈
- LintCode-剑指Offer-(12)带最小值操作的栈
- [LintCode] 带最小值操作的栈 Min Stack
- [未解决][lintcode]带最小值操作的栈
- (lintcode)第12题带最小值操作的栈
- [LintCode 12] 带最小值操作的栈(Python)
- Jquery show方法不起作用解决方案
- 澄清核函数(kernel)的几个问题
- 深层神经网络结构及可能存在的致命问题详解
- Android笔记-2:重读单例模式有感
- MAC算法总结
- Google/LintCode:M-带最小值操作的栈
- HTTP请求错误码表
- 退出所有Activity
- Caffe Layers
- CDN
- HDOJ1428 BFS+DFS 记忆化搜索裸题
- javaWeb之-------统计网站刷新量
- 深入浅出Swarm
- 唯有读书和思考使我归于平静