3.2---最小栈(CC150)
2015-12-20 13:24
302 查看
//思路:入栈时不是最小值,永远都没机会成为最小值。 import java.util.Stack; class MinStack { private Stack<Integer> stack = new Stack<Integer>(); private Stack<Integer> minStack = new Stack<Integer>(); public void push(int x) { stack.push(x); if(!minStack.empty()) { int min = minStack.peek(); if(x <= min) { minStack.push(x); } } else { minStack.push(x); } } public void pop() { if(minStack.size() != 0 && ((int)stack.peek() == (int)minStack.peek())) { minStack.pop(); } stack.pop(); } public int top() { return (int)stack.peek(); } public int getMin() { return (int)minStack.peek(); } }
相关文章推荐
- 图层中与输入几何形相交的要素
- Leetcode: Valid Anagram
- HDU 2138 How many prime numbers
- Python程序无法在命令行下运行
- installsheild2011打包程序internal build error 6213
- It seems that this file has not been built yet.Do you want to build it now? Codeblocks能构建build不能运run
- XML 文档的结构
- 浦发银行微信支付/绑定状态异常,请解绑后重新绑定
- QT学习之路(2):基础学习(1)
- URL编解码
- Beta函数与Gamma函数及其与Beta分布的关系
- HDOJ 2124 Repair the Wall
- AngularJs 指令实现选项卡
- 3.1---一个数组实现三个栈(CC150)
- Appium的一点一滴:Mobile JSON Wire Protocol
- Logitech的SetPointP控制网易云音乐,千千静听
- autorelease详解
- ASP.NET Global 全局事件处理
- Appium的一点一滴:获取Android Activity三种方法
- js设置cookie