《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 案例分析与实现 基本操作有入栈.出栈.读栈顶元素值.建栈.判断栈满.栈空 ...
随机推荐
- 检测浏览器中是否有Flash插件
由于IE和非IE浏览器检测方式不同,所以代码如下 function hasPlugin(name){ debugger; name = name.toLowerCase(); for(var i=0; ...
- [转载]开启debug调试模式
debug+trace模式可以查看开发过程中TP的错误信息,可以更好地帮助开发者debug.但是debug模式的开启还不是简单的在配置文件中中设置就可以的,经过查资料摸索,找到一种有效的方法. 首先在 ...
- Jenkins配置(Jenkins如何与maven项目进行连用)
一同事,在问关于Jenkins搭建后,他是如何与我们项目进行连用的,如何通过Jenkins去编译我们的项目的,现在介绍下如何通过Jenkins持续编译我们的项目 配置过程 1.确定我们已经搭建好了Je ...
- Ajax(一):XHR的用法
AJAX能够向服务器请求额外的数据而无须卸载页面,会带来更好的用户体验. 1.在使用xhr对象时,要调用都第一个方法就是open(),它接收3个参数:要发送的请求的类型(get,post等).请求的u ...
- Android检查更新(是否强制更新)
Android应用客户端通常会需要更新,而且根据需求分为普通更新和强制更新.是否强制更新可通过检查更新时从服务器获取的标志位来判断. public class UpdateManager { priv ...
- redis string类型
- js循环读取json数据,将读取到的数据用js写成表格
①js循环读取json数据的方式: var data=[{"uid":"2688","uname":"*江苏省南菁高级中学 022 ...
- 堆(heap)和栈(stack)几点认识
堆(heap)和栈(stack)主要的区别由以下几点:1.管理方式不同:2.空间大小不同:3.产生碎片不同:4.生长方向不同:5.分配归属不同:6.分配效率不同:7.存取效率不同:管理方式:对于栈来讲 ...
- 连接MYSQL 错误代码2003
问题是服务里面mysql没有启动或者mysql服务丢失 解决办法: 开始->运行->cmd,进到mysql安装的bin目录(以我的为例,我的安装在D盘)D:\MySQL\bin>my ...
- python 获取项目的根路径
root_path = os.path.abspath(os.path.dirname(__file__)).split('shippingSchedule')[0] shippingSchedule ...