leetcode——155—— Min Stack
2016-04-02 10:51
381 查看
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.
Subscribe to see which companies asked this question
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.
Subscribe to see which companies asked this question
class MinStack { public: stack<int> stk; stack<int> min; void push(int x) { stk.push(x); if(min.empty() || x <= min.top()) min.push(x); } void pop() { if(stk.top() == min.top()) { stk.pop(); min.pop(); } else stk.pop(); } int top() { return stk.top(); } int getMin() { return min.top(); } };
相关文章推荐
- 直方图均衡化源码
- rpm包管理以及前端工具yum
- 蓝鲸python第一题个人理解运行版
- 让数组中奇数位于偶数前面
- 码农小汪之-线程的基本概念
- 想成为更优秀的程序员?这8个方法帮你忙
- 5-2 然后是几点 (15分)
- 直方图均衡化
- leetcode 283. Move Zeroes
- HDU 1875 畅通工程再续 最小生成树
- 【JQuery】使用wrap()和wrapInner()方法包裹元素和内容
- HDU 1875 畅通工程再续 最小生成树
- C语言18个经典问题答录
- fragment的使用
- 前端代码的一些基本规范使用
- Android 消息循环机制源码分析
- Nagle算法
- [Android实例] 推荐给你们一个好用的ListView、RecyclerView适配器
- Android 6.0 wif的变动
- 【笔试/面试】数组及其内存结构