2014-03-18 05:33

题目:用两个栈来实现一个队列。

解法:栈是反的,队列是正的,反了再反就正过来了。所以,请看代码。操作中时间复杂度有O(1)的,有O(n)的,但均摊下来时间符合O(1)。

代码:

 // 3.5 Implement a queue MyQueue using two stacks.
#include <climits>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std; template<class T>
class MyQueue {
public:
bool empty() {
return s1.empty() && s2.empty();
} void push(const T &val) {
s1.push(val);
} void pop() {
if (s2.empty()) {
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
}
s2.pop();
} T front() {
if (s2.empty()) {
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
} return s2.top();
} T back() {
if (s1.empty()) {
while (!s2.empty()) {
s1.push(s2.top());
s2.pop();
}
} return s1.top();
}
private:
stack<T> s1, s2;
}; int main()
{
MyQueue<int> qq;
char s[];
int op; while (scanf("%s", s) == ) {
if (strcmp(s, "end") == ) {
break;
} else if (strcmp(s, "push") == ) {
scanf("%d", &op);
qq.push(op);
} else if (strcmp(s, "pop") == ) {
qq.pop();
} else if (strcmp(s, "front") == ) {
printf("%d\n", qq.front());
} else if (strcmp(s, "back") == ) {
printf("%d\n", qq.back());
}
} return ;
}

《Cracking the Coding Interview》——第3章:栈和队列——题目5的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

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

  7. 数据结构(c语言版,严蔚敏)第3章栈和队列

    第3章栈和队列

  8. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  9. Cracking the Coding Interview 150题(二)

    3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...

  10. 数据结构(C语言版)-第3章 栈和队列

    3.1 栈和队列的定义和特点3.2 案例引入3.3 栈的表示和操作的实现3.4 栈与递归3.5 队列的的表示和操作的实现3.6 案例分析与实现 基本操作有入栈.出栈.读栈顶元素值.建栈.判断栈满.栈空 ...

随机推荐

  1. 利用PCHunter结束各种进程

    http://www.epoolsoft.com/ 经测试,可结束主动防御等.

  2. java文件

    File类 为了很方便的代表文件的概念,以及存储一些对于文件的基本操作,在java.io包中设计了一个专门的类——File类. 在File类中包含了大部分和文件操作的功能方法,该类的对象可以代表一个具 ...

  3. 119. Pascal's Triangle II (Amazon) from leetcode

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

  4. chromedp自动启动为headless模式

    // Command click is a chromedp example demonstrating how to use a selector to // click on an element ...

  5. POJ-1195 Mobile phones---裸的二维树状数组(注意下标从1,1开始)

    题目链接: https://vjudge.net/problem/POJ-1195 题目大意: 直接维护二维树状数组 注意横纵坐标全部需要加1,因为树状数组从(1,1)开始 #include<c ...

  6. 2018.8.18 servlet使用的会话跟踪除session外还有哪些方式

    解释HTTP HTTP是一种无连接的协议,如果一个客户端只是单纯地请求一个文件(HTML或GIF),服务器端可以响应给客户端,并不需要知道一连串的请求是否来自于相同的客户端,而且也不需要担心客户端是否 ...

  7. 复杂链表的复制 -python编写

    题目描述 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否 ...

  8. p2597 灾难

    我的思路 代码: #include<cstdio> #include<iostream> #include<algorithm> #include<vecto ...

  9. apache单ip配置多端口多站点

    1.修改 /etc/httpd/conf/httpd.conf 添加一个Listen,如: Listen 80 Listen 8001 Listen 8002 2.添加一个VirtualHost #v ...

  10. 第38章 I2S—音频播放与录音输入—零死角玩转STM32-F429系列

    第38章     I2S—音频播放与录音输入 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/ ...