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 ...
随机推荐
- IPC low/medium/high density 什么意思?
http://wiki.altium.com/pages/viewpage.action?pageId=3080344 Land Pattern Information Density Level A ...
- 三分钟教你学Git (四)之紧急救助
假设你不小心git reset --hard HEAD^ 然后这个commit又没有在别的git仓库中,怎么办?是不是这次改动就丢了呢? 当然不是,git为我们每次都历史都保留了reference l ...
- mysql 语句or效率问题
今天看一同事代码中sql语句的拼接,看到where column=? or column=? .... 一直循环遍历下去,即根据传递进来的数组长度构造sql查询(mysql库) for(int i= ...
- 【spring boot】整合LCN,启动spring boot2.0.3 启动报错:Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
spring boot 2.0.3启动报错: Error starting ApplicationContext. To display the conditions report re-run yo ...
- Appium+python自动化20-查看iOS上app元素属性
前言 学UI自动化首先就是定位页面元素,玩过android版的appium小伙伴应该都知道,appium的windows版自带的Inspector可以定位app上的元素 Mac版的appium1.6的 ...
- java jdk查看源代码
事实上假设你安装了JDK的话,你就已经拥有了java api的源代码. 安装JDK文件夹下的src.zip文件就是java api的源代码. 比方:C:\Program Files\Java\jdk1 ...
- [转]MySQL Explain
Mysql Explain 详解 一.语法 explain < table_name > 例如: explain select * from t3 where id=3952602; 二. ...
- ThreadLocal实现session中用户信息 的线程间共享
转载自:http://blog.sina.com.cn/s/blog_4b5bc01101013gok.html ThreadLocal并不难理解,我总结的最简单的理解就是: ThreadLocal像 ...
- Liunx下文件权限详解
刚接触Linux时对Linux下的文件权限的概念一直很模糊,观念还一直停留在windows下,所以有很多操作一直提示权限不够.为了弄懂文件权限问题我查找了很多资料整理如下,我把这些学习笔记贴出来和大家 ...
- Unity 打包发布Android新手教学 (小白都能看懂的教学 ) [转]
版权声明:本文为Aries原创文章,转载请标明出处.如有不足之处欢迎提出意见或建议,联系QQ531193915 扫码关注微信公众号,获取最新资源 最近在Unity的有些交流群里,发现好多Unity开发 ...