使用队列实现栈的操作

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的更多相关文章

  1. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  2. 【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( ...

  3. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  4. 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 ...

  5. 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) -- ...

  6. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  7. 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 ...

  8. Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  9. (leetcode)Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  10. (easy)LeetCode 225.Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

随机推荐

  1. Navicat Premium基本使用

    Navicat Premium基本使用 转自:https://blog.csdn.net/Yangchenju/article/details/80633055 Navicat Premium基本使用 ...

  2. 实验吧CTF练习题---WEB---貌似有点难解析

    实验吧web之貌似有点难   地址:http://www.shiyanbar.com/ctf/32 flag值:SimCTF{daima_shengji}   解题步骤: 1.打开题目页面,观察题目要 ...

  3. <xsl:apply-templates>和<xsl:call-template>的区别

    <xsl:apply-templates> 应用模板,故名思意,将定义好的模板应用到 XML 的节点上.  可以调用 XML 文档的节点,使 XSL 文档可以渲染 XML 元素内的数据,  ...

  4. NTP服务器实现

    时间服务器是一种计算机网络仪器,它从参考时钟获取实际时间,再利用计算机网络把时间信息传递给用户.虽然还有一些比较少用或过时的协议仍然在使用,但现时最重要及广泛使用,作为时间信息发送和同步化的协议是网络 ...

  5. Redis常用命令(key、string、List)

    1.Key 1.keys *   查询所有数据 2.exists key名   判断key名是否存在 3.move key名  数据库号(0-15)  移动数据key名到相应的数据库 4.expire ...

  6. 《Java7并发编程实战手册》读书笔记

    一.线程管理 1.线程的创建和运行 创建线程的2种方式: 继承Thread类,并覆盖run()方法 创建一个实现Runnable接口的类.使用带参数的Thread构造器来创建Thread对象 每个Ja ...

  7. [LeetCode]sum合集

    LeetCode很喜欢sum,里面sum题一堆. 1.Two Sum Given an array of integers, return indices of the two numbers suc ...

  8. 修改tomcat 使用的JVM的内存

    一,前言 在文章让tomcat使用指定JDK中,我让tomcat成功使用了我指定的JDK1.8,而不是环境变量中配置的JDK10.本篇文章我们就来探讨一下怎么设置tomcat使用的JVM的内存. 为什 ...

  9. rocketmq学习(二) rocketmq集群部署与图形化控制台安装

    1.rocketmq图形化控制台安装 虽然rocketmq为用户提供了使用命令行管理主题.消费组以及broker配置的功能,但对于不够熟练的非运维人员来说,命令行的管理界面还是较难使用的.为此,我们可 ...

  10. Maven项目运行报错提示找不到加载主类

    遇到这个问题花了几小时时间看网上的各种解决方法.试了几种都没用,最后用了这种本办法. 亲测第三条 https://blog.csdn.net/yuliantao/article/details/766 ...