Leetcode 155 Min Stack
题意:设计一个能输出栈内最小值的栈
该题设计两个栈,一个栈是正常的栈s,而另一个是存最小值的栈sm
在push时要判断sm是否为空,如果为空或者非空但是栈顶元素大于等于插入值的 需要在sm中插入x
同样地在pop时,s的元素被删除了,那么sm中的也应该被删除。
通过这些操作维护sm能很巧妙在O(1)复杂度得到最小值。
class MinStack {
public:
stack<int> sm;
stack<int> s;
void push(int x) {
s.push(x);
if(sm.empty() || (!sm.empty() && sm.top() >= x)) sm.push(x);
}
void pop() {
if(s.top() == sm.top()) sm.pop();
s.pop();
}
int top() {
return s.top();
}
int getMin() {
return sm.top();
}
};
Leetcode 155 Min Stack的更多相关文章
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- LeetCode 155 Min Stack(最小栈)
翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Java for LeetCode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Leetcode 155 Min Stack 小顶堆+栈,优先队列实现 难度:0
https://leetcode.com/problems/min-stack/ #include <vector> #include <queue> #include < ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
随机推荐
- Xml文件操作的其中一个使用方法:
XmlNodeList students = doc.DocumentElement.ChildNodes;//Student节点集合 foreach (XmlNode stu in students ...
- PHP--字符串处理函数
字符串的声明 1. 2. 3. [注]单引号与双引号声明字符串的区别: 1.strlen():获取字符串长度 2.substr():截取字符串 3.strpos():查找字符串在指定字符串中的位置 4 ...
- VedioCaptureHelper
void testFun() { chStringA strDevName; chStringA strDevID; chStringA useDevName = "WIN2 USB2.0 ...
- 2015年九月八日---js学习总结
参考书:javaScript Dom 编程的艺术 知识小结:一:js简史:前称ECMAScript 是一种脚本语言通常只能通过浏览器进行解释和执行. Dom(文档对象模型):是一套对文档的内容进行抽象 ...
- 咏南IOCP REST中间件
咏南IOCP REST中间件 让DELPHI7也能编写REST服务. 使用IOCP通信+UNIDAC数据库引擎. 客户端跨开发语言调用.
- java JVM垃圾回收机制
Java语言出来之前,大家都在拼命的写C或者C++的程序,而此时存在一个很大的矛盾,C++等语言创建对象要不断的去开辟空间,不用的时候有需要不断的去释放控件,既要写构造函数,又要写析构函数,很多时候都 ...
- linux 清理内存命令 查看内存命令
查看内存: 我们可以用free命令查看内存信息: free -g total used free shared buffers cachedMem: 15 15 0 0 ...
- LeetCode OJ-- Wildcard Matching **@
https://oj.leetcode.com/problems/wildcard-matching/ 模拟通配符的匹配 做法非常好 class Solution { public: bool isM ...
- Entity Framework Code First 学习
1.添加entityframework 项目-管理解决方案的 NuGet 程序包-联机-Entity Framework 2.code first Migration 工具->库程序包管理器-& ...
- java中的匿名内部类小结
匿名内部类也就是没有名字的内部类.正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写. 但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口. 实例1:不使用匿名内部类来实现 ...