《Cracking the Coding Interview》——第3章:栈和队列——题目1
2014-03-18 03:19
题目:用一个数组实现3个栈。
解法:
首先我想过让三个栈动态决定长度。要么左右各一个向中间靠拢,要么三个穿插着,后来都觉得实现起来太复杂,而且思路总有各种功能缺陷,会导致额外的时间或空间复杂度。所以,还是三等分成固定大小吧。好写又好用。
代码:
// 3.1 Use an array to implement three stacks.
// three fixed-length stacks
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std; template <class T>
class ThreeStack {
public:
ThreeStack(size_t _total_capacity = ): total_capacity(_total_capacity) {
msize[] = ;
msize[] = ;
msize[] = ; mcapacity[] = mcapacity[] = mcapacity[] = total_capacity / ;
mcapacity[] += (total_capacity % != ? : );
mcapacity[] += (total_capacity % == ? : ); offset[] = ;
offset[] = offset[] + mcapacity[];
offset[] = offset[] + mcapacity[]; mdata.resize(total_capacity);
} ~ThreeStack() {
mdata.clear();
} void push(int idx, T val) {
if (msize[idx] == mcapacity[idx]) {
// this stack is full
return;
} mdata[offset[idx] + msize[idx]] = val;
++msize[idx];
} void pop(int idx) {
if (msize[idx] == ) {
return;
} --msize[idx];
} T top(int idx) {
if (msize[idx] == ) {
return mdata[-];
} return mdata[offset[idx] + msize[idx] - ];
} size_t size(int idx) {
return msize[idx];
}
private:
// total capacity of all stack
size_t total_capacity;
// starting offset for each stack
size_t offset[];
// capacities of the three stacks
size_t mcapacity[];
// sizes of the three stacks
size_t msize[];
// the data in the stacks
vector<T> mdata;
}; int main()
{
int n;
size_t idx;
int val;
char str[]; scanf("%d", &n);
ThreeStack<int> ts(n);
while (scanf("%s", str) == ) {
if (strcmp(str, "end") == ) {
break;
} else if (strcmp(str, "push") == ) {
scanf("%u%d", &idx, &val);
ts.push(idx, val);
} else if (strcmp(str, "pop") == ) {
scanf("%u", &idx);
ts.pop(idx);
} else if (strcmp(str, "top") == ) {
scanf("%u", &idx);
printf("top[%u] = %d\n", idx, ts.top(idx));
} else if (strcmp(str, "size") == ) {
scanf("%u", &idx);
printf("size[%u] = %u\n", idx, ts.size(idx));
}
} return ;
}
《Cracking the Coding Interview》——第3章:栈和队列——题目1的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 数据结构(c语言版,严蔚敏)第3章栈和队列
第3章栈和队列
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- Cracking the Coding Interview 150题(二)
3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...
- 数据结构(C语言版)-第3章 栈和队列
3.1 栈和队列的定义和特点3.2 案例引入3.3 栈的表示和操作的实现3.4 栈与递归3.5 队列的的表示和操作的实现3.6 案例分析与实现 基本操作有入栈.出栈.读栈顶元素值.建栈.判断栈满.栈空 ...
随机推荐
- April 19 2017 Week 16 Wednesday
What would life be if we had no courage to attempt anything? 如果我们都没有勇气去尝试点什么,生活会变成什么样子呢? I remembere ...
- nodejs+MQTT协议实现远程主机控制
摘抄自百度:MQTT(MessageQueuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,有可能成为物联网的重要组成部分. 所谓物联网,就是“万物互 ...
- *15. 3Sum (three pointers to two pointers), hashset
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- Jmeter入门5 关联 响应数据传递-正则表达式提取器
在测试过程中,遇到一个问题:用户登录成功后服务器会返回一个登录凭证,之后所有的操作都需要带上此凭证.我们怎么获取登录凭证并传递给后续的操作? Jmeter提供了正则表达式提取器,用变量提取参数,后续通 ...
- IOS 设置颜色的的详情
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- python里json的排序
写一下json排序的问题: 将得到的数据转换成json格式传给ajax,会发现前台得到的数据很有可能和我们在后台的内容’不太一样‘,如果不出意外,json进行了自排序处理,至于按什么格式排的我还没研究 ...
- url获取MVC域,action,controller的方法
域:filterContext.RequestContext.RouteData.DataTokens["area"] 控制器:filterContext.RequestConte ...
- 创建一个gradle项目
1.创建项目 一定要选这个安装的路径 项目创建成功,修改build.gradle文件,主要是为了下载依赖的jar包,原始模板, 而我修改之后,如下 apply plugin: 'idea' apply ...
- C#自制png转ico图标工具
此项目基于.net framework 4.0 只需把图片拖拽到窗口内,自动转换生成ico图标,在png文件同级目录下. /// /// 实现代码: 转换Image为Icon /// ///要转换为图 ...
- CUDA三维数组
http://hpcbbs.it168.com/forum.php?mod=viewthread&tid=1643 根据上面链接的帖子研究了下三维数组,就像他自己说的一样是有问题的,我自己修改 ...