leetcode--Min Stack
2015-06-06 00:12
302 查看
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 { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int x) { if(stack2.size()==0||x<stack2.peek()){ stack2.add(x); }else{ stack2.add(stack2.peek()); } stack1.add(x); } public void pop() { stack2.pop(); stack1.pop(); } public int top() { return stack1.peek(); } public int getMin() { return stack2.peek(); } }
相关文章推荐
- Android自定义控件
- linux调度器源码分析 - 初始化(二)
- linux 怎样查找某个文件或目录属于哪个分区的。
- Java interface 做函数参数
- Java for LeetCode 171 Excel Sheet Column Number
- 对象与类的认识
- File 随笔 ( 1 )
- 有关子类构造函数的问题
- 信息论与编码 上机
- windbg调试dmp文件
- .NET获取指定文件夹下的所有文件
- Java for LeetCode 169 Majority Element
- linux调度器源码分析 - 概述(一)
- Arduino程序设计-C基础
- 现代密码学实践指南[2015年]
- 控制验证码在前台的显示
- RealtekRTL8111内建网卡-黑苹果之路
- dp问题最佳路径的构建 poj 1015
- leetcode--Valid Palindrome
- GDI+ - 使用PNG图片打造不规则窗口