Min Stack
来源:互联网 发布:大数据数据库选择 编辑:程序博客网 时间:2024/05/23 20:41
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.
- 代码如下:
- class MinStack {
private Stack<Integer>stack =new Stack<Integer>();
private Stack<Integer>minstack=new Stack<Integer>();
public void push(int x) {
if(minstack.isEmpty()||x<=minstack.peek())
minstack.push(x);
stack.push(x);
}
public void pop() {
if(stack.peek().equals(minstack.peek()))
minstack.pop();
stack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minstack.peek();
}
} - 参考博文:http://blog.csdn.net/ljiabin/article/details/40982153
0 0
- min stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- Min Stack
- 《做自己》+《爱生活》收到了,感谢七哥
- 播放视频
- jenkins安装和配置
- 答辩体会 2015/01/24
- 把数学踢出高考,那么谁家的孩子该学数学?
- Min Stack
- Cocos2d-js调用java/ios返回错误码-4是什么错误
- OpenCV 人脸检测自学(3)
- Redis高可用部署及监控
- StackOverflow程序员推荐:每个程序员都应读的30本书
- an internal error occurred during building workspace错误
- 将一种格式的文件读入,以另一种格式写出文件
- ClassLoader 详解及用途(写的不错)
- eclipse (CDT 4.4 Luna)c++11 shared_ptr报错问题