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, ...
随机推荐
- Mysql Case when 语句
首先我们创建一列sex.再为部分行设置好值0(女)或者1(男): 现在要做这样一件事,查询显示的时候sex不显示0,1和null,我们让它0的就显示女,1的就显示男,null就显示未知. 这时我们 ...
- Spring常用表单验证注解
下面是主要的验证注解及说明: 注解 适用的数据类型 说明 @AssertFalse Boolean, boolean 验证注解的元素值是false @AssertTrue Boolean, boole ...
- Maven中的库(repository)详解
Maven中的库(repository)是构件(artifact)的集合.构件以一定的布局存储在库中. 本地仓库 vs. 远程仓库 运行Maven的时候,Maven所需要的任何构件都是直接从本地仓库获 ...
- C#中的枚举(Enum)你知道多少呢?
写个随笔文章是最难想的,我要是写个C#枚举个人小结,估计博客园的各位园有也觉得是哪个刚接触C#的人写的,要是取个名字叫C#枚举,又觉得不能完全表达自己的意思,现在这个名字看起来还凑合吧,写篇文章不容易 ...
- 初学 Delphi 嵌入汇编[1] - 汇编语言与机器语言
非科班出身, 现在才接触汇编, 惭愧呀, 好好学! 主选课本是清华大学王爽老师的<汇编语言>. 推荐 王爽老师的汇编网 汇编语言之前是机器语言. 机器语言是机器指令的集合, 机器指令是一系 ...
- 大数据开发实战:Spark Streaming流计算开发
1.背景介绍 Storm以及离线数据平台的MapReduce和Hive构成了Hadoop生态对实时和离线数据处理的一套完整处理解决方案.除了此套解决方案之外,还有一种非常流行的而且完整的离线和 实时数 ...
- @class指令的使用
@class指令能够减少编译时间,告诉编译器“相信我,你最终能了解这个名称的类”,可以减少不得不导入的头文件的数量. sample如下: #import <Foundation/Foundati ...
- Linux命令行极简教程
1.命令行真的好吗 程序员的使命 维基百科的解释: 命令行界面(英语:command-line interface,缩写:CLI)是在图形用户界面得到普及之前使用最为广泛的用户界面,它通常不支持鼠标, ...
- Sql控制反转小尝试
假想用配置Sql语句的方式来完毕一个处理逻辑,而且映射到一个Url,这样当请求这个url的时候,运行前面配置的sql. 以下的一段详细配置,比如 当请求pagerlistdept.do的时候,会传入參 ...
- [Docker] Converting from Docker Compose to Kubernetes
kompose is a tool to help users who are familiar with docker-compose move to Kubernetes. kompose tak ...