Leetcode21: Min Stack
来源:互联网 发布:java移植安卓游戏 编辑:程序博客网 时间:2024/05/17 01:58
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: std::stack<int> stack; std::stack<int> minstack;public: void push(int x) { stack.push(x); if(minstack.empty() || x <= minstack.top()) minstack.push(x); } void pop() { if(!stack.empty()) { if(minstack.top() == stack.top()) { minstack.pop(); } stack.pop(); } } int top() { if(!stack.empty()) { return stack.top(); } } int getMin() { if(!minstack.empty()) { return minstack.top(); } }};
0 0
- Leetcode21: 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
- jag2014autumn F Reverse a Road II 网络流
- HDU 5215 Cycle(判定无向图奇偶环)
- aSmack源码分析register过程分析
- Go语言周边常用命令积累
- Leetcode第二题_Add Two Numbers
- Leetcode21: Min Stack
- 神经网络(2)
- 算法之LCA与RMQ问题
- iOS8中使用CoreLocation定位
- LINUX ulimit命令 详解
- 神经网络(3)
- smack 源码分析一(android上实现长连接)【1】
- Json字符串和highcharts数据图表展现
- JDK中的URLConnection参数详解