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. 音乐代码 (DNF天空之城、欢乐颂)。

    太感人了 DNF天空之城 #include <cstdio> #include <windows.h> #define qdo 262 #define qre 294 #def ...

  2. Html : 将submit变成像文字一样的按钮

    直接上代码: <html> <head> <title>像文字一样的按钮</title> <style> body{ background- ...

  3. 用户表单事件(focus事件)

    以前做用户系统的时候经常用到表单验证,正则表达式事件来处理和绑定事件和进行事件,这里说的其实只是一小部分,也不是很值得写,但是今天遇到了还是写一下,毕竟基础还是蛮重要的,就算懂的童鞋,巩固一下也是好的 ...

  4. cesium 显示视角高度以及鼠标经纬度

    HTML中的内容 <div id="cesiumContainer"> <!-- 设置经纬度显示 --> <span style="font ...

  5. IOS 音频的 使用说明

    说明 ● 简单来说,音频可以分为2种 ● 音效 • 又称“短音频”,通常在程序中的播放时长为1~2秒 • 在应用程序中起到点缀效果,提升整体用户体验 ● 音乐 • 比如游戏中的“背景音乐”,一般播放时 ...

  6. 45. 腾讯面试题: 使用hashmap 插入数据,怎么样依照插入数据的顺序输出数据

    题目:使用hashmap 插入数据,怎么样依照插入数据的顺序输出数据 分析: 使用hashmap插入数据,数据的顺序会改变.能够写个小程序试试. 那怎么样依照插入的顺序输出呢? 方法一: 这是我第一时 ...

  7. java常用输出技巧,debug

    package control; import javax.swing.JFrame; public class DebugTest { public static void main(String[ ...

  8. 2017.9.29 web网上答题及其自动评测系统

    1. 设计计一个网上答题及其自动评测系统,首先是试题页面的设计及其解答的提交, 其次是当提交解答之后,系统自动评阅并给出结果. 分析:需要两个jsp页面:一个是提交信息的页面,另一个是获取提交信息的页 ...

  9. Laravel5 构造器高级查询条件写法

    <?php #DB 高级查询 // select * from table where A and B or C $all_data = DB::table("shopnc_goods ...

  10. 谷歌浏览器兼容IE插件

    谷歌浏览器兼容IE插件 http://pan.baidu.com/s/1i31hspf