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 ...
随机推荐
- python简介及详细安装方法
1.Python简介 1.1 Python是什么 相信混迹IT界的很多朋友都知道,Python是近年来最火的一个热点,没有之一.从性质上来讲它和我们熟知的C.java.php等没有什么本质的区别,也是 ...
- (六十七)c#Winform自定义控件-柱状图
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- java对象与java对象引用的区别
java对象与java对象引用的区别 对象与对象引用的区别 直接用例子说话吧 Person per = new Person("张三"); 这一条语句,其实包括了四个动作: 右边的 ...
- VG有空间,创建逻辑卷
1.查看VG空间 [root@CNSZ22PL2787 ~]# vgs VG #PV #LV #SN Attr VSize VFree VolGroup00 1 7 0 wz--n- 1.63t 1. ...
- SpringBoot修改默认端口号 及 上下文
- Winform中在FastReport的PreviewControl预览控件中对report控件模板中控件值进行修改
场景 FastReport安装包下载.安装.去除使用限制以及工具箱中添加控件: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- Redis的复制(Master/Slave)、主从复制、读写分离 (下)
哨兵模式(就是反客为主的自动版) 能够自动监控主机是否故障,如果故障了根据投票数自动将从机变成主机 1创建哨兵文件 touch sentinel.conf [root@localhost myredi ...
- Visual Studio Code编写C/C++代码常见问题
我会把一些常见问题以及自己编写代码过程中遇到的问题以及解决方案放在这里,各位若是遇到的问题也可以在评论区留言. 一.头文件Error 不会影响编译运行,但会报Warm,如下图 解决方案是安装Inclu ...
- linux 操作系统级别监控 TOP命令
Top命令是Linux下一个实时的.交互式的,对操作系统整体监控的命令,可以对CPU.内存.进程监控. 是Linux下最常用的监控命令. 第一行是任务队列信息 1 user 当前登录用户数load a ...
- Mysql - 关于relay_log_recovery参数的测试
一.概述 官方文档中对relay_log_recovery参数的解释 Enables automatic relay log recovery immediately following server ...