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的更多相关文章

  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. 双击易语言没有反应,按住shift再双击可解决

    参考资料:http://tieba.baidu.com/p/2987732743 的7楼.

  2. May 1 2017 Week 18 Monday

    The very essence of romance is uncertainty. 浪漫的精髓就在于它充满了种种可能. Yesterday my girl friend told me that ...

  3. 那些年我用过的SAP IDE

    在Google上根据关键字"程序员鄙视链"搜索,会得到68多万条结果. 玲琅满目的搜索结果里是众多不同维度划分的鄙视链. 其中有一个维度,就是编程工具的鄙视链,比如: 而我在SAP ...

  4. Aizu The Maximum Number of Customers

    http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_5_A The Maximum Number of Customers Ide ...

  5. IOS 控制当前控制器支持哪些方向

    #pragma mark - 实现这个方法来控制屏幕方向 /** * 控制当前控制器支持哪些方向 * 返回值是UIInterfaceOrientationMask* */ - (NSUInteger) ...

  6. ABI and compiler

    http://stackoverflow.com/questions/2171177/what-is-application-binary-interface-abi ABIs cover detai ...

  7. 虚拟内存映射 段分割 vm_area_struct

    http://www.cnblogs.com/huxiao-tee/p/4660352.html linux内核使用vm_area_struct结构来表示一个独立的虚拟内存区域,由于每个不同质的虚拟内 ...

  8. F​l​a​s​h​ ​M​e​d​i​a​ ​L​i​v​e​ ​E​n​c​o​d​e​r​参​数​表

    Flash Media Live Encoder命令行推流Flash Media Live Encoder NotesFlash Media Live Encoder 除了直接以 GUI 方式操作之外 ...

  9. mysql [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GRO

    [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c ...

  10. 2018.9.3 CEP课程总结

    什么是CEP课程? 职业生涯规划课程 蓝桥介绍 立人达人 全人教育 人文 重视人 尊重人 关心人 爱护人 人才 人格 简历的制作 找工作的流程? 1.简历的准备------>投发简历(自己投.老 ...