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 ...
随机推荐
- Tasker 中文版(4.6u2)
http://www.anzhi.com/soft_1868913.html Tasker绝对称得上是Android系统的神器之一,与Auto Memory Manager不同,Tasker不是加速型 ...
- Spring Bean init-method 和 destroy-method实例
在Spring中,可以使用 init-method 和 destroy-method 在bean 配置文件属性用于在bean初始化和销毁某些动作时.这是用来替代 InitializingBean和Di ...
- 用 C# 做人脸检测(基于EmguCV)
c#datagridmatlab人脸识别图像处理path 下载源码 准备工作(必须) 下载 EmguCV 傻瓜安装后,把 bin 目录添加到环境变量里去,比如安装在 X:\EmguCV\ 目录下,就 ...
- SharePoint Server 2013 Offline Installation (without Internet)
转自:http://social.msdn.microsoft.com/Forums/sharepoint/zh-CN/08f90e0f-1f52-4eba-9f6e-4dd635ffaadc/sha ...
- /dev/fb0入门练习
转:http://hi.baidu.com/fsx92/item/bdbf90d8adab409f260ae738 大家都知道Unix/Linux系统是由命令驱动的.那么最基本的系统是命令行的(就是想 ...
- OPENGL架构
第2章 OpenGL 简介 每台计算机都有专门处理图形的硬件,它们控制着屏幕上显示的内容.OpenGL向这种硬件发出命令,告诉它们执行什么操作.计算机游戏或者其他任意软件借助制造商提供的设备驱动程序, ...
- Labeled Faces in the Wild 人脸识别数据集 部分测试数据
development test set Note: images displayed are original (non-aligned/funneled) images. match pairs ...
- utf8_general_ci、utf8_unicode_ci和utf8_bin的区别
utf8_general_ci 不区分大小写,这个你在注册用户名和邮箱的时候就要使用. utf8_general_cs 区分大小写,如果用户名和邮箱用这个就会照成不良后果. utf8_bin: com ...
- Mysql优化与使用集锦
MyISAM的读性能是比Innodb强 MyISAM的索引和数据是分开的,并且索引是有压缩的 Innodb是索引和数据是紧密捆绑的,没有使用压缩从而会造成Innodb比MyISAM体积庞大不小 MyI ...
- Linux Centos7安装chrome浏览器
参考:https://blog.csdn.net/u010472499/article/details/72327963 1. 配置yum源 在目录 /etc/yum.repos.d/ 下新建文件 g ...