[leedcode 155] Min Stack
2015-08-01 22:29
405 查看
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
getMin() -- Retrieve the minimum element in the stack.
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
getMin() -- Retrieve the minimum element in the stack.
class MinStack { //使用两个栈,一个正常使用,一个保存已经压栈中最小的数,每次push和pop注意两个栈同时操作 private Stack<Integer> min=new Stack<Integer>(); private Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); if(min.empty()||x<min.peek()){//注意min判空 min.push(x); }else{ min.push(min.peek()); } } public void pop() { stack.pop(); min.pop(); } public int top() { return stack.peek(); } public int getMin() { return min.peek(); } }
相关文章推荐
- MyBatis知多少(25)动态SQL
- Letter Combinations of a Phone Number
- 手动安装fastboot驱动
- 转吧总结,一些零零碎碎的东西
- 几种开源工作流引擎的简单比较(转)
- java中volatitle关键字的作用
- Rails 极简入门笔记
- js map, reduce, forEach, filter的一般实现
- thinkpad T430s 在win8上安装ubuntu双系统(UEFI开启)
- 计数排序
- 领域驱动设计系列(转)
- losersaver硕士待遇杂谈
- nginx配置
- UVA_11552_FewestFlops
- uC/OS-II 函数之OSInit()
- struts2中jsp页面向后台传值的三种方法
- 最简单的基于FFmpeg的移动端例子:Windows Phone HelloWorld
- springmvc使用校验框架validation校验
- 小马哥----高仿酷派8732定制版刷机拆机主板多图与开机界面展示 6582芯片
- Gray Code