LeetCode 155 Min Stack(最小栈)
翻译
设计支持push、pop、top和在常量时间内检索最小元素的栈。
push(x) —— 推送元素X进栈
pop() —— 移除栈顶元素
top() —— 得到栈顶元素
getMin() —— 检索栈的最小元素
原文
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.
分析
之前写过两道题,各自是用Stack来实现Queue的功能以及用Queue来实现Stack的功能,这里假设也使用两个Stack的话就很easy了。
LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
LeetCode 232 Implement Queue using Stacks(用栈来实现队列)(*)
class MinStack {
public:
stack<int> allStack;
stack<int> minSta;
void push(int x) {
if (allStack.empty()) {
allStack.push(x);
minSta.push(x);
}
else {
allStack.push(x);
if (x <= minSta.top()) minSta.push(x);
}
}
void pop() {
if (allStack.top() == minSta.top()) {
minSta.pop();
}
allStack.pop();
}
int top() {
return allStack.top();
}
int getMin() {
return minSta.top();
}
};
除了上面的stack,我还用vector实现了:
class MinStack {
public:
vector<int> allVec;
vector<int> minVec;
void push(int x) {
if (allVec.empty()) {
allVec.push_back(x);
minVec.push_back(x);
}
else {
if (x <= minVec[minVec.size() - 1]) {
minVec.push_back(x);
}
allVec.push_back(x);
}
}
void pop() {
if (allVec[allVec.size() - 1] == minVec[minVec.size() - 1])
minVec.erase(minVec.end() - 1);
allVec.erase(allVec.end() - 1);
}
int top() {
return allVec[allVec.size() - 1];
}
int getMin() {
return minVec[minVec.size() - 1];
}
};
然而用vector效率反而更低了……
LeetCode 155 Min Stack(最小栈)的更多相关文章
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- 【LeetCode】155. Min Stack 最小栈 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
- 155 Min Stack 最小栈
设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈. push(x) -- 将元素x推入栈中. pop() -- 删除栈顶的元素. top() -- 获取 ...
- [CareerCup] 3.2 Min Stack 最小栈
3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...
- 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] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- lintcode 中等题:Min stack 最小栈
题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
随机推荐
- VC中__int64的用法
VCVC6.0的64位整数分别叫做__int64与unsigned __int64,其范围分别是[-2^63, 2^63)与[0,2^64),即-922337203685 4775808~922337 ...
- Ubuntu中升极下载4.2内核
http://tech.hexun.com/2015-09-11/179027013.html 从这段话中所表达出的意思可以了解,Linux Kernel 4.3版本已经开始进行,Linus Torv ...
- .Net4.0并行库介绍——Cancellation Framework
在.net 4.0中,引入了一个新的类CancellationToken,这个类基本上集成了我们各种常用的取消方式,在并发任务中非常有用. 同步模式下的取消: 一种比较常见的需要支持取消功能的的是一些 ...
- 树状数组求第K小值 (spoj227 Ordering the Soldiers && hdu2852 KiKi's K-Number)
题目:http://www.spoj.com/problems/ORDERS/ and pid=2852">http://acm.hdu.edu.cn/showproblem.php? ...
- UIButton 设置圆角 边框颜色 点击回调方法
UIButton *signBtn = [UIButton buttonWithType:UIButtonTypeCustom]; signBtn.frame = CGRectMake(, , , ) ...
- 错误 1 error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use . See online help for details.
出现这种警告的原因是因为我们没有使用安全的字符串处理函数.如果想屏蔽这种警告,可以使用: 还可以使用其它的方法,参考: https://www.cnblogs.com/gb2013/archive/2 ...
- 【iOS开发-90】CALayer图层:自己定义图层,图层的一些动画
(1)效果 (2)代码 http://download.csdn.net/detail/wsb200514/8261547 (3)总结 --能够自己定义图层,尤其须要对图片进行圆角裁剪. --图层的动 ...
- Android -- Activity的销毁和重建
两种销毁 第一种是正常的销毁,比如用户按下Back按钮或者是activity自己调用了finish()方法: 另一种是由于activity处于stopped状态,并且它长期未被使用,或者前台的acti ...
- uva 10518 - How Many Calls?(矩阵快速幂)
题目链接:uva 10518 - How Many Calls? 公式f(n) = 2 * F(n) - 1, F(n)用矩阵快速幂求. #include <stdio.h> #inclu ...
- (转)Loader ,URLLoader ,URLStream的区别
AS3代码 (1) Loader Loader 类可用于加载 SWF 文件或图像(JPG.PNG 或 GIF)文件. 使用 load() 方法来启动加载. 被加载的显示 ...