LeetCode_225-Implement Stack using Queues
使用队列实现栈的操作
class MyStack {
public:
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
if(!queueA.empty()) {
queueA.push(x);
}
else {
queueB.push(x);
}
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int nTmp = ;
if(queueA.empty()) {
while(queueB.size() > ) {
queueA.push(queueB.front());
queueB.pop();
}
nTmp = queueB.front();
queueB.pop();
}
else {
while(queueA.size() > ) {
queueB.push(queueA.front());
queueA.pop();
}
nTmp = queueA.front();
queueA.pop();
}
return nTmp;
}
/** Get the top element. */
int top() {
int nTmp = ;
if(queueA.empty()) {
while(!queueB.empty()) {
nTmp = queueB.front();
queueA.push(queueB.front());
queueB.pop();
}
}
else {
while(!queueA.empty()) {
nTmp = queueA.front();
queueB.push(queueA.front());
queueA.pop();
}
}
return nTmp;
}
/** Returns whether the stack is empty. */
bool empty() {
if(queueA.empty() && queueB.empty()) {
return true;
}
return false;
}
protected:
queue<int> queueA;
queue<int> queueB;
};

可关注公众号了解更多的面试技巧
LeetCode_225-Implement Stack using Queues的更多相关文章
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- 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 ...
- Implement Queue by Two Stacks & Implement Stack using Queues
Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...
- [LeetCode] 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 ...
- Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (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 ...
随机推荐
- 【Offer】[8] 【中序遍历的下一个结点】
题目描述 思路分析 Java代码 代码链接 题目描述 给定一棵二叉树和其中的一个结点,如何找出中序遍历顺序的下一个结点? 树中的结点除了有两个分别指向左右子结点的指针以外,还有一个指向父结点的指针. ...
- css3-旋转的太极图
123 body { background-color: #aaa; } .div { width: 400px; height: 400px; border-radius: 50%; border: ...
- debug 模式缓慢
debug 模式启动服务器,然后在 breakopints下可以看到打的断点.清除全部重启服务器,问题解决.
- springboot中动态修改logback日志级别
springboot中动态修改logback日志级别 在spring boot中使用logback日志时,项目运行中,想要修改日志级别. 代码如下: import org.slf4j.Logger; ...
- 初始mqtt服务
MQTT入门 概念 mqtt意为消息队列遥测传输,是IBM开发的一个即时通讯协议.由于其维护一个长连接以轻量级低消耗著称,所以常用于移动端消息推送服务开发. 协议格式 mqtt协议控制报文的格式包含三 ...
- 【原创】我们还需要学jQuery吗?
引言 最近撸Vue的项目,感觉的有点心累.恰巧近日,有读者来信,就是想咨询一下 烟哥,现在还有必要学习jQuery么? 我明白,现在MVVM框架逐渐占据了主要市场,很多老项目也逐渐的从jQuery转向 ...
- Redis删除集群以及重新启动集群
有时候我们搭建完集群以后,对集群进行了一些错误的操作,导致集群出现了不可预料的问题,这时候想要删除集群重新启动一个原始的集群,那么如何删除原来旧的集群呢? 1.关闭所有开启的Redis节点 kill ...
- Graph and Chart Study
1.选择主题 “Themes”,以"Graph"——“preset7” 为例: 2.选择“Canvas”——“GraphChart”预制体,1.Set data categorie ...
- java生成二维码/java解析二维码
二维码的优缺点 优点:1. 高密度编码,信息容量大:2.编码范围广:3.容错能力强:4.译码可靠性高:5.可引入加密措施:6.成本低,易制作,持久耐用. 缺点:1.二维码技术成为手机病毒.钓鱼网站传播 ...
- MYSQL之概念基础篇
1数据库概述 1.1 数据管理技术的产生和发展 数据库技术是应数据库管理任务的需要而产生的.20世纪50年代中期以前,计算机主要是用于科学计算.当时的硬件状况是,外存只有纸带.卡片.磁带,没有磁盘等可 ...