《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 案例分析与实现 基本操作有入栈.出栈.读栈顶元素值.建栈.判断栈满.栈空 ...
随机推荐
- ring0 进程隐藏实现
最近在学习内核编程,记录一下最近的学习笔记. 原理:将当前进程从eprocess结构的链表中删除 无法被! process 0 0 看见 #include "HideProcess.h&qu ...
- OpenGL学习 Following the Pipeline
Passing Data to the Vertex Shader Vertex Attributes At the start of the OpenGL pipeline,we use the i ...
- java 内存举例
1. java内存的主要划分 2. OOTest02.java 的内存划分 public class OOTest02{ public static void main(String[] args) ...
- 为什么CRM Opportunity的删除会触发一个通向BW系统的RFC
今天工作时我发现,我在SE38里用函数CRM_ORDER_DELETE删除一个Opportunity,居然弹出下图这个SAP Logon的屏幕,要连接BR1.这是什么鬼?! 查了一下,BR1是BW系统 ...
- 2017.11.24 算法分析与设计------Gay格雷码
1. 格雷码问题: 对于给定的正整数n,格雷码为满足如下条件的一个编码序列: (1) 序列由2n个编码组成,每个编码都是长度为n的二进制位串. (2) 序列中无相同的编码. (3) 序列中位置相邻的两 ...
- Python中的__name__和__main__含义详解
1背景 在写Python代码和看Python代码时,我们常常可以看到这样的代码: ? 1 2 3 4 5 def main(): ...... if __name == "__m ...
- System.Web.UI.Page
mdsn:点击查看此类介绍 git: 点击查看封装方法 消息弹框,消息弹框跳转,自定义脚本信息 定义:表示一个从托管 ASP.NET Web 应用程序的服务器请求的 .aspx 文件(也称为 ...
- linux服务器安装nginx及使用
Nginx在个人的使用之后,感觉非常的方便,所以在这里给出自己安装配置方案.它是一款高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器.负载均衡是个不错的选择. ...
- 短信状态监听 - iOS
当使用 App 时若短信介入需要对当前状态进行监听操作,根据不同的状态实行相关的需求操作,废话不多说步骤如下. 首先,常规操作先引用对应的头文件,来为后续功能铺路. #import <Messa ...
- 字符串替换For linux C
1.临时空间给了个1024,不需要可减少长度. 2.结果只用用strcpy了,没校验. bool Replace(char *str,const char *src, const char *des) ...