LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译
用队列来实现栈的例如以下操作。
push(x) —— 将元素x加入进栈
pop() —— 从栈顶移除元素
top() —— 返回栈顶元素
empty() —— 返回栈是否为空
注意:
你必须使用一个仅仅有标准操作的队列。
也就是说,仅仅有push/pop/size/empty等操作是有效的。
队列可能不被原生支持。这取决于你所用的语言。
仅仅要你仅仅是用queue的标准操作,你能够用list或者deque(double-ended queue)来模拟队列。
你能够如果全部的操作都是有效的(比如,pop或peek操作不会被用在空栈上)。
原文
Implement the following operations of a stack using queues.
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
empty() -- Return whether the stack is empty.
Notes:
You must use only standard operations of a queue
-- which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively.
You may simulate a queue by using a list or deque (double-ended queue),
as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example,
no pop or top operations will be called on an empty stack).
分析
对栈和队列不清楚的话。能够先看这篇图文介绍:【算法】7 分不清栈和队列?一张图给你完总体会
至于这道题目。有一道很很相似的题目。
本题是用队列来实现栈,以下这题是用栈来实现队列。由于在上一篇中解说过,原理是一样的,大家能够自己看看:LeetCode 232 Implement Queue using Stacks(用栈来实现队列)(*)
代码
class Stack {
public:
queue<int> q, q2;
// Push element x onto stack.
void push(int x) {
q.push(x);
}
// Removes the element on top of the stack.
void pop() {
if (q.size() == 1) q.pop();
else {
while (q.size() > 1) {
q2.push(q.front());
q.pop();
}
q.pop();
while (q2.size() > 0) {
q.push(q2.front());
q2.pop();
}
}
}
// Get the top element.
int top() {
if (q.size() == 1) return q.front();
while (q.size() > 1) {
q2.push(q.front());
q.pop();
}
int temp = q.front();
q2.push(q.front());
q.pop();
while (q2.size() > 0) {
q.push(q2.front());
q2.pop();
}
return temp;
}
// Return whether the stack is empty.
bool empty() {
return q.empty();
}
};
LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)的更多相关文章
- [LeetCode] 225. Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- LeetCode 225 Implement Stack using Queues 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Leetcode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java [Leetcode 225]Implement Stack using Queues
题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...
- Leetcode 225 Implement Stack using Queues STL
用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的 push时插入到空的队列中,然后将队列中的元素移到另一个队列中 pop时从不空的队列中pop() peek时从不空的 ...
- 225 Implement Stack using Queues(用队列实现栈Medium)
题目意思:用队列实现栈,push(),pop(),top(),empty() 思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop, ...
随机推荐
- WorkerThread与MainThread之间通过Handler进行最简单的消息传递
一.从自己开启的线程中给主线程发送信息,更新UI 这个实例的效果是,在线程中通过handler发送一条信息给handler,然后通过handler更改UI线程中,textview的文字. 主要方法是( ...
- JavaScript中的方法、方法引用和参数
首先,我们来看一段代码,如果觉得不甚明白的,则本文会对你有益: var player = function (e) { return (function f(m) { ...
- java.lang.NoClassDefFoundError: com.sap.conn.jco.JCo (initialization failure) java.lang.UnsatisfiedLinkError: no sapjco3 in java.library.path
java.lang.NoClassDefFoundError: com.sap.conn.jco.JCo (initialization failure) at java.lang.J9VMInter ...
- hdu 2544 单源最短路问题 dijkstra+堆优化模板
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 混沌分形之迭代函数系统(IFS)
IFS是分形的重要分支.它是分形图像处理中最富生命力而且最具有广阔应用前景的领域之一.这一工作最早可以追溯到Hutchinson于1981年对自相似集的研究.美国科学家M.F.Barnsley于198 ...
- [转]教你修复win7中复制粘贴失效的问题
教你修复win7中复制粘贴失效的问题 发布时间:2018-01-17 使用win7系统的时候,我们经常需要对立面的内容进行复制粘贴来引用一些网站的内容,不过最近有网友在使用这个 ...
- 阿里NLP总监分享-NLP技术的应用与思考
https://yq.aliyun.com/articles/78031 NLP技术的应用及思考
- HTML JS 数据校验
用到了html字符串校验,这里记录一下. <html> <head> <script type="text/javascript"> funct ...
- 插件化 VirtualAPK 简介 体验 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 纯css解决div隐藏浏览器原生滚动条,但保留鼠标滚动效果的问题
当我们的内容超出了我们的div,往往会出现滚动条,影响美观.尤其是当我们在做一些导航菜单的时候.滚动条一出现就破坏了UI效果. 我们不希望出现滚动条,也不希望超出去的内容被放逐,就要保留鼠标滚动的效 ...