155. Min Stack
2016-03-09 20:19
197 查看
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 { public: stack<int> y1; stack<int> y2; void push(int x) { y1.push(x); if((y2.empty())||(x<y2.top())) y2.push(x); else { int z=y2.top(); y2.push(z); } } void pop() { y2.pop(); y1.pop(); } int top() { return y1.top(); } int getMin() { return y2.top(); } };
相关文章推荐
- onCreate
- subline text3快捷键
- 14. Longest Common Prefix 第二个稍微好一些
- 234. Palindrome Linked List
- 303. Range Sum Query - Immutable
- C++ 按指定分隔符拆分字符串
- 【清华集训2014】mex
- 清醒吧拭心!自满的程序员是最菜的!
- 判断有向图中是否有环
- JavaScript 核心基础语法
- 【数据结构】线性表(链表实现)
- 【高性能】linux加载当前目录的动态库
- 28. Implement strStr()
- need improve204. Count Primes
- Java网络编程——InetAddress
- AngularJS+Node的RESTful之基本实现
- 24点游戏&&速算24点(dfs)
- 常用网络管理操作
- [C]LeetCode:Maximum Depth of Binary Tree
- C++虚继承中的对象内存布局