题意是使用栈实现队列;队列是先进先出,后进后出。

class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {
//intput
m_bSwap = true;
} /** Push element x to the back of queue. */
void push(int x) {
if(!m_bSwap) {
while(!m_soutput.empty()) {
m_sintput.push(m_soutput.top());
m_soutput.pop();
}
m_bSwap = true;
}
m_sintput.push(x);
} /** Removes the element from in front of queue and returns that element. */
int pop() {
if(m_bSwap) {
while(!m_sintput.empty()) {
m_soutput.push(m_sintput.top());
m_sintput.pop();
}
m_bSwap = false;
}
int nNum = m_soutput.top();
m_soutput.pop();
return nNum;
} /** Get the front element. */
int peek() {
if(m_bSwap) {
while(!m_sintput.empty()) {
m_soutput.push(m_sintput.top());
m_sintput.pop();
}
m_bSwap = false;
}
return m_soutput.top();
} /** Returns whether the queue is empty. */
bool empty() {
if(m_sintput.empty() && m_soutput.empty()) {
return true;
}
return false;
} protected:
stack<int> m_sintput;
stack<int> m_soutput;
bool m_bSwap;
};

可关注公众号了解更多的面试技巧

LeetCode_232-Implement Queue using Stacks的更多相关文章

  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. Lintcode: Implement Queue by Stacks 解题报告

    Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...

  6. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  7. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

  8. [LeetCode] Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  9. Leetcode Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  10. Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

随机推荐

  1. d3.js 实现烟花鲜果

    今天在d3.js官网上看到了一个烟花的DEMO,是canvas制作的,于是我想用d3.js来实现它,js代码只有几行.好了废话不多说,先上图. 1 js 类 因为烟花要有下落的效果,所以里面用到了一些 ...

  2. 通过脚本实现将服务器的Log实时传送到Telegram群组

    首先说下需求,IT老大提出的一个需求,实现将php-laravel的应用日志实时传送到telegram的监控群组中,不用登陆服务器就可以实时查看应用的日志. 具体思路是: 先要将日志切割,并实时更新这 ...

  3. Java 中创建对象的 5 种方式!

    Java中有5种创建对象的方式,下面给出它们的例子还有它们的字节码 Employee类: class Employee implements Cloneable, Serializable { pri ...

  4. IDEA中工程上传到SVN

    1.先在IDEA上集成SVN 2.查看SVN仓库:连接SVN ①此时应该先去SVN服务器中新建一个SVN服务: 点击下一步 点击下一步 点击create ②查看SVN仓库 先复制SVN的地址 把复制的 ...

  5. 在命令行已经pip install flask-script,但是导包时出错

    问题:(已经安装好了flask-script,但是导入不成功) 然后在代码中导入相应的包:(报红) 后来发现是在自己创建项目的时候勾选的是创建的是在虚拟环境下的项目,所以环境有问题 所以我应该在虚拟环 ...

  6. (七十五)c#Winform自定义控件-控件水印组件

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  7. Eclipse通过SVN导入项目遇到的问题记录

    问题一.把子项目导入为project 原因:一个大的文件夹,里面有各个小项目,需要把自己添加需求的醒目导入为Maven Project 1.右键选 Import as project 2.右键 -&g ...

  8. 这个注册的 IP 网络都不通了,Eureka 注册中心竟然无法踢掉它!

    本文导读: 微服务技术架构选型介绍 k8s 容器化部署架构方案 Eureka 注册中心问题场景 问题解决手段及原理剖析 阅读本文建议先了解: 注册中心基本原理 K8s(Kuberneters)基本概念 ...

  9. 报错:ORA-25150:不允许对区参数执行ALERING

    alter table 表名 MOVE   storage   (     next 128   ) ; -- Add/modify columns alter table 表名 add 列名 var ...

  10. Java查询判断素数实验报告

    实验源代码: package sushu; import java.util.Scanner; public class First { int size=2; int data[]=new int[ ...