leetcode Min Stack 最小栈
来源:互联网 发布:去除淘宝店铺模块缝隙 编辑:程序博客网 时间:2024/06/05 20:12
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、pop、top和返回最小值的操作
思路:用两个栈实现,其中一个栈维持每次push和pop之后的最小值。
class MinStack {private: stack<int> s1; stack<int> s2;public: /** initialize your data structure here. */ void push(int x) { s1.push(x); if (s2.empty() || x <= getMin()) s2.push(x); } void pop() { if (s1.top() == getMin()) s2.pop(); s1.pop(); } int top() { return s1.top(); } int getMin() { return s2.top(); }};/** * Your MinStack object will be instantiated and called as such: * MinStack obj = new MinStack(); * obj.push(x); * obj.pop(); * int param_3 = obj.top(); * int param_4 = obj.getMin(); */
阅读全文
0 0
- leetcode Min Stack 最小栈
- leetcode Min Stack 最小栈
- [LeetCode] - Min Stack O(1)最小栈
- LeetCode 155 Min Stack(最小栈)
- LeetCode 155. Min Stack(最小栈)
- Min Stack最小栈
- Min Stack 最小栈
- leetcode 155. Min Stack 最小栈的实现
- LeetCode[stack]: Min Stack
- [leetcode][stack] Min Stack
- Min Stack(返回栈中最小数)
- Min Stack 实现一个最小栈
- [leetcode]Min Stack (获取栈的最小元素C语言实现)
- 【LeetCode-面试算法经典-Java实现】【155-Min Stack(最小栈)】
- 【Leetcode】Min Stack
- Leetcode: Min Stack
- leetcode Min stack
- Min Stack -- LeetCode
- 动态规划和背包dp问题
- 大型高性能ASP.NET系统架构设计
- Python网络爬虫与信息提取(二):网络爬虫之提取
- OpenCV-Python—图像平滑和模糊
- 《她身之欲》(珠三角阅流动人口社群特殊职业研究)阅读感想
- leetcode Min Stack 最小栈
- IAR Embebdded Workbench IDE转移设置到另一部电脑中
- Hibernate与spring整合不能建表的问题
- (crm笔记)阶段一总结
- 如何搭建lamp(CentOS7+Apache+MySQL+PHP)环境
- c++位运算
- MySQL日期的加减
- swift学习日记
- MySQL命令-- mysqlbinlog