LeetCode 155:Min Stack
来源:互联网 发布:网络性爱小说 编辑:程序博客网 时间:2024/06/05 17:23
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.
依然是用两个栈来满足。其中一个栈正常存放所有元素,另一个栈存放当前栈内的最小元素(在pop的时候需要判断)
class MinStack {public: stack<int> stk1; stack<int> stk2; void push(int x) { stk1.push(x); if(stk2.empty()||stk2.top()>=x) stk2.push(x); } void pop() { if(stk1.top()==stk2.top()) stk2.pop(); stk1.pop(); } int top() { return stk1.top(); } int getMin() { return stk2.top(); }};
0 0
- LeetCode(155) Min Stack
- [leetcode 155] Min Stack
- leetCode #155 Min Stack
- LeetCode[155]Min Stack
- leetcode-155 Min Stack
- leetcode 155 Min Stack
- LeetCode[155]-Min Stack
- leetcode 155:Min Stack
- LeetCode---(155)Min Stack
- leetcode-155-Min Stack
- leetcode 155: Min Stack
- [leetcode] #155 Min Stack
- LeetCode 155 Min Stack
- 【LEETCODE】155-Min Stack
- LeetCode 155:Min Stack
- [leetcode 155] Min Stack
- leetcode 155: Min Stack
- 【leetcode】【155】Min Stack
- 机房重构之数据库设计
- linux内核代码的编写初步以及makefile的配置
- iOS MD5加密和SHA-1加密
- Ext.js5(编辑表格的内容celledit)(添加和删除的行为)(20)
- MVC5学习系列——HelloWorld
- LeetCode 155:Min Stack
- 51nod 1403 有趣的堆栈 (找规律题)
- 性能测试知多少---吞吐量
- Oracle 12c里面select view权限没有的解决方法
- 复制自身程序到Windows目录和系统目录下
- Java生成随机编码
- android自定义图标字体的使用
- 性能测试知多少---并发用户
- MongoDB 3.2版WiredTiger存储引擎性能测试