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

  2. [转载]——Axure+SVN配置

    最近进行考试系统重构,一个小组十几个人,这么多人要同时搞需求画原型.这样原本的合作开发工具SVN已经不能满足现在的需求了,这是就找到了一个新的方法——Axure+SVN. 在SVN服务器端建立一个空的 ...

  3. 将一个命令的输出保存到CSV文件

    执行段: 结果段: 补充:配合不同的命令可以使工作更加简单 使用Imort-Csv命令从文件中导入结构化数据

  4. lasagne保存网络参数

    # Optionally, you could now dump the network weights to a file like this: # np.savez('model.npz', *l ...

  5. convolution,fft, 加速

    零零星星挖坑几个了,都没填土,实在是欠账太多,闲话少说吧,还是多记录总结一下.今天的主题是围绕convolution和加速 记得之前看过lecun他们组的一篇文章,是fft加速convolution的 ...

  6. 树梅派3B kali2.0 启用SSH进行远程登录

    工具/原料 kali 2.0 ssh SSH连接工具(XShell)or PUTTY vi /etc/ssh/sshd_config 将#PasswordAuthentication no的注释去掉, ...

  7. 经典sql语句汇总

    1,某条数据放首位,其他倒序并分页 select * from Student order by( case     when id='2'  then 1 ELSE 4 END),id desc l ...

  8. 浅谈MySQL字符集

      Preface       MySQL use character set & collation to organize the different charater.It provid ...

  9. java后台poi根据模板导出excel

    public class ExcelUtils { private static final String INSPECTIONRECORD_SURFACE_TEMPLET_PATH = " ...

  10. django+xadmin在线教育平台(七)

    4-3 新建项目 Python2.7 创建虚拟环境. mkvirtualenv mxonline2 安装django pip install django==1.9.8 注意Python2下此处必须用 ...