1.

说明如何用两个队列来实现一个栈,并分析有关栈操作的运行时间。

解法:
1.有两个队列q1和q2,先往q1内插入a,b,c,这做的都是栈的push操作。
2.现在要做pop操作,即要得到c,这时可以将q1中的a,b两个元素全部dequeue并存入q2中,这时q2中元素为a,b,对q1再做一次dequeue操作即可得到c。
3.如果继续做push操作,比如插入d,f,则把d,f插入到q2中,
4.此时若要做pop操作,则做步骤2
5.以此类推,就实现了用两个队列来实现一个栈的目的。

注意在此过程中,新push进来的元素总是插入到非空队列中,空队列则用来保存pop操作之后的那些元素,那么此时空队列不为空了,原来的非空队列变为空了,总是这样循环。

说明如何用两个z栈来实现一个队列。

Chp3: Stacks and Queue的更多相关文章

  1. LeetCode -- Implement Stacks using Queue

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

  2. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  3. 面试题目——《剑指Offer》

    1.把一个字符串转换成整数——<剑指Offer>P29 2.求链表中的倒数第k个结点——<剑指Offer>P30 3.实现Singleton模式——<剑指Offer> ...

  4. WPF异步载入图片,附带载入中动画

    原文:WPF异步载入图片,附带载入中动画 WPF异步载入图片,附带载入中动画 最近,在做一个WPF项目.项目中有一个需求,就是以列表的方式显示出项目图片.这些图片有的存在于互联网上,有的存在于本地磁盘 ...

  5. 【剑指offer】Java实现(持续更新中)

    面试题3 二维数组中的查找 Leetcode--74 Search a 2D Matrix /*Java Write an efficient algorithm that searches for ...

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

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

  7. Leetcode Implement Queue using Stacks

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

  8. Implement Queue using Stacks

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

  9. [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列

    3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...

随机推荐

  1. Javascript之换肤(未完待续)

    这个项目我还没有完全写出来,先记录至此.感觉是方法不对,背景图片的切换方法有Problem.如若有一大神发现了我的文章,还望指导,吾将感激不尽.日后代码还会再钻研再改改. <head> & ...

  2. Aliyun OSS SDK 异步分块上传导致应用异常退出

    问题描述: 使用Aliyun OSS SDK的BeginUploadPart/EndUploadPart执行异步分块上传操作,程序出现错误并异常退出! 原因分析: Using .NET Framewo ...

  3. SharePoint Server 2013介绍v2

    SharePoint Server 2013介绍v2 http://wenku.baidu.com/link?url=rNYYoDmHcXmqhzOeA1sln5cS2xcnABYycBuz8Z4Oq ...

  4. OC2_点语法(属性关键字)

    // // Dog.h // OC2_点语法(属性关键字) // // Created by zhangxueming on 15/6/16. // Copyright (c) 2015年 zhang ...

  5. 【译】 Node.js v0.12的新特性 -- 性能优化

    原文: https://strongloop.com/strongblog/performance-node-js-v-0-12-whats-new/ January 21, 2014/in Comm ...

  6. Redis配置中文翻译,3.2.1版

    大部分常见设置都翻译了,还有一些是从网上复制的(懒) # Redis configuration file example. ## Redis配置文件示例 # # Note that in order ...

  7. opencv 手写选择题阅卷 (四)Android端 手机应用开发

    opencv 手写选择题阅卷 (四)Android 手机应用开发 在PC端把代码调通以后开始开发Android 手机应用,因为主要功能代码为C++代码,所以需要通过NDK编译,JAVA通过JNI方式调 ...

  8. oracle 之路目录

    oracle linux单机安装 oracle windows单机安装创建实例卡死解决办法 oracle rac安装 HPDL380G8平台11.2.0.3 RAC实施手册 pl-sql develo ...

  9. 判断浏览器js代码

    (function(){ var UA = {}; var ua = navigator.userAgent.toLowerCase(),s; UA.ie = (s = ua.match(/(msie ...

  10. PHP开发环境和软件

    1/很方便的软件XAMMP集成了PHP+MYSQL+MYPHPADMIN等等软件 2/sublime text 程序员神器,都明白的 ps.如果装了vm虚拟机,80端口有时候会被占用,进程关闭就好.