3.1 Describe how you could use a single array to implement three stacks.

这道题让我们用一个数组来实现三个栈,书上给了两种方法,第一种方法是定长分割 Fixed Division,就是每个栈的长度相同,用一个公用的一位数组buffer来保存三个栈的内容,前三分之一为第一个栈,中间三分之一为第二个栈,后三分之一为第三个栈,然后还要分别记录各个栈当前元素的个数,然后再分别实现栈的基本操作push, pop, top 和 empty,参见代码如下:

class ThreeStacks {
public:
ThreeStacks(int size) : _stackSize(size) {
_buffer.resize(size * , );
_stackCur.resize(, -);
} void push(int stackIdx, int val) {
if (_stackCur[stackIdx] + >= _stackSize) {
cout << "Stack " << stackIdx << " is full!" << endl;
}
++_stackCur[stackIdx];
_buffer[stackIdx * _stackSize + _stackCur[stackIdx]] = val;
} void pop(int stackIdx) {
if (empty(stackIdx)) {
cout << "Stack " << stackIdx << " is empty!" << endl;
}
_buffer[stackIdx * _stackSize + _stackCur[stackIdx]] = ;
--_stackCur[stackIdx];
} int top(int stackIdx) {
if (empty(stackIdx)) {
cout << "Stack " << stackIdx << " is empty!" << endl;
}
return _buffer[stackIdx * _stackSize + _stackCur[stackIdx]];
} bool empty(int stackIdx) {
return _stackCur[stackIdx] == -;
} private:
int _stackSize;
vector<int> _buffer;
vector<int> _stackCur;
};

第二种方法是灵活分割 Flexible Divisions,这种方法较为复杂,这里就不细写了。

[CareerCup] 3.1 Implement Three Stacks using Array 使用数组来实现三个栈的更多相关文章

  1. golang之 Array(数组)

    目录 一.Array(数组) 二.数组的定义 1. 基本语法 三.数组的初始化 1. 方式一 2. 方式二 3. 方式三 四.数组的遍历 1. 方式一:for循环遍历 2. 方式二:for range ...

  2. vector以及array和数组

    //比较数组.vector.array #include <iostream> #include <vector> #include <array> #includ ...

  3. Array(数组)对象-->概念和创建

    1.什么是数组? 数组对象是使用单独的变量名来存储一系列的值. 2.数组创建的三种方法: 方法1:常规方式 var arr=new Array(); arr[0]="lisa"; ...

  4. java中Array(数组)的用法

    8.Array(数组)    数组是作为对象来实现的.(really occupy the memopry,真实的占用内存 ) An array is a data structure that st ...

  5. [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列

    3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...

  6. [CareerCup] 17.6 Sort Array 排列数组

    17.6 Given an array of integers, write a method to find indices m and n such that if you sorted elem ...

  7. [CareerCup] 3.3 Set of Stacks 多个栈

    3.3 Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in ...

  8. [CareerCup] 7.4 Implement Multiply Subtract and Divide 实现乘法减法和除法

    7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only ...

  9. [CareerCup] 8.1 Implement Blackjack 实现21点纸牌

    8.1 Design the data structures for a generic deck of cards. Explain how you would subclass the data ...

随机推荐

  1. 我们需要专职的QA吗?

    [ 引用评论里的一句话:hurt but true  抛开作者某些偏激的想法外,作者暴露出来的问题还是需要测试思考的: 1.TestCase,TestData,TestConfiguration 没有 ...

  2. Effective Java 35 Prefer annotations to naming patterns

    Disadvantages of naming patterns Typographical errors may result in silent failures. There is no way ...

  3. Tomcat如何添加管理员

    为Tomcat添加管理员,然后用管理员登录后就可以看到所有加载的工程包,以及运行的平台,还可以对项目进行管理,比如删和添加. 一.工具:apache-tomcat-7.0.39 二.添加步骤 1. 首 ...

  4. Windows 系统下json 格式的日志文件发送到elasticsearch

    Windows 系统下json 格式的日志文件发送到elasticsearch配置 Nxlog-->logstash-->ElasticSearch Logstash https://ww ...

  5. zip文件jQuery工作地点选择城市代码

    效果 地址下载:http://download.csdn.net/detail/xiaoliu123586/9201925 2.效果 源码:http://download.csdn.net/detai ...

  6. Java基础の第二弹 基础语法

    Java关键字 •  abstract:表明类或类中的方法是抽象的:•  boolean:基本数据类型之一,布尔类型:•  break:提前跳出一个块:•  byte:基本数据类型之一,字节类型:•  ...

  7. dev/shm time in linux

    统计文件夹大小: du -hx --max=1 : du -sk :du -hsc 重新组织行分隔符进行显示: echo "abc,dd,bach,dong,jing,shang,china ...

  8. OpenXml 入门----OpenXml Tools使用技巧

    简介: Office2007以上版本的文档其实可以转换为XML格式.截图如下: Test.doc 解压过后已经完全变为文件夹和xml文件,文档的属性和信息都存储在了xml里面.根据XML就封装出了Op ...

  9. RPC(远程过程调用)的应用

    接触背景 因为工作上某项目的需要设计一种分布式处理耗时的运算,每个节点然后将运算结果返回给中心服务器,而最初未了解RPC这部分之前我的设计是在每一个RPC服务器上搭建一个webserver,然后部署运 ...

  10. java日期和字符串的相互转换

    日期->字符串,字符串->日期:日期->毫秒,毫秒>日期- private static void getDate() { // TODO Auto-generated met ...