LeetCode_232-Implement Queue using Stacks
题意是使用栈实现队列;队列是先进先出,后进后出。
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的更多相关文章
- 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 ...
 - Lintcode: Implement Queue by Stacks   解题报告
		
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
 - Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
		
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
 - LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
		
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
 - [LeetCode] Implement Queue using Stacks 用栈来实现队列
		
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
 - Leetcode Implement Queue using Stacks
		
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
 - Implement Queue using Stacks
		
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
 
随机推荐
- Fire Balls 10——UI界面的制作
			
版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...
 - Maven学习归纳(四)——传递依赖和依赖的规则
			
一.传递依赖 官方文档解释的传送门:http://ifeve.com/maven-dependency-mechanism/ 当存在传递依赖的情况时,主工程对间接依赖的jar可以访问吗? 例如:A.j ...
 - tensorflow 使用 cpu 而不使用 gpu 问题
			
查看 tensorflow 版本 conda list 例如发现 tensorflow 1.10.0 tensorflow-gpu 1.10.0 当两个版本相同时,默认会使用 cpu 版本 如果同时存 ...
 - HTML 框架导航
			
初次学习HTML,在www.w3school.com.cn看到了框架导航,上面的例子没有看懂所以搜了一下相应的问题,最后弄懂了怎么实现同一界面下的框架导航. 首先是www.w3school.com.c ...
 - 年年有余之java求余的技巧集合
			
背景 传说里玉皇大帝派龙王马上降雨到共光一带,龙王接到玉皇大帝命令,立马从海上调水,跑去共光施云布雨,但粗心又着急的龙王不小心把海里的鲸鱼随着雨水一起降落在了共光,龙王怕玉皇大帝责怪,灵机一动便声称他 ...
 - Java中自定义注解类,并加以运用
			
在Java框架中,经常会使用注解,而且还可以省很多事,来了解下自定义注解. 注解是一种能被添加到java代码中的元数据,类.方法.变量.参数和包都可以用注解来修饰.注解对于它所修饰的代码并没有直接的影 ...
 - WordCount程序【Spark Streaming版本】
			
~~ 前置 ~~ Spark Streaming 常常对接 :本地文件.HDFS.端口.flume.kafka package february.streaming import org.apache ...
 - VMware CentOS7 安装
			
一.软硬件准备 作者:小啊博 QQ:762641008 转载请声明:https://www.cnblogs.com/-bobo 1.准备Centos7镜像 软件:推荐使用VMwear,我用的是VMwe ...
 - springboot系列之04-提高开发效率必备工具lombok
			
未经允许,不得转载 原作者:字母哥博客 本文完整系列出自:springboot深入浅出系列 一.前置说明 本节大纲 使用lombok插件的好处 如何安装lombok插件 使用lombok提高开发效率 ...
 - Jedis 常用API使用
			
使用Jedis操作Redis常用的API <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <depen ...