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. android intent 5.1

    1.intent 6 items action, data(uri &type),Component name,Extras,flags 2.data---uri & type 不管使 ...

  2. HTTP/HTTPs要点

    Http(超文本传输协议)是一个属于应用层的面向对象的协议. HTTP结构 HTTP请求: 请求行:Method-Request URI HTTP-Version(CRLF),eg:GET /form ...

  3. [LoadRunner]性能测试实践_Hessian协议脚本编写2

    协议选取和运行配置请参考:http://www.cnblogs.com/whylaughing/p/5430821.html 这次直接贴代码让大家参考: import lrapi.lr; import ...

  4. 【API】短信通106端口验证短信的实现

    信息时代,无论是电商还是网络营(chuan)销(xiao)都希望得道更多的用户信息.所以很多的网站注册上用到了手机验证码功能.网上有很多的SMS接口提供商.在选择的时候无非就是考虑到1.发送速度:2. ...

  5. CentOS 7 修改时区(转)

    本文转载至:http://mathslinux.org/?p=637 Linux 系统(我特指发行版, 没说内核) 下大部分软件的风格就是不会仔细去考虑向后 的兼容性, 比如你上个版本能用这种程序配置 ...

  6. 被废弃的 Thread.stop, Thread.suspend, Thread.resume 和Runtime.runFinalizersOnExit

    最近学习多线程的知识,看到API里说这些方法被废弃了,就查了一下原因 Thread.stop 这个方法会解除被加锁的对象的锁,因而可能造成这些对象处于不一致的状态,而且这个方法造成的ThreadDea ...

  7. 烂泥:【解决】Ubuntu下使用SSH连接centos系统很慢

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 这几天在Ubuntu下使用SSH连接centos系统,发现连接很慢.建议一个连接大约需要30s.很是坑爹,如下: 后来查询相关资料,发现这个是Ubunt ...

  8. Busybox下mdev配置说明

    [TOC] mdev说明 mdev是busybox自带的一个简化版的udev,适合嵌入式应用场合.其具有使用简单的特点.它的作用就是在系统启动和热插拔或动态加载驱动程序时,自动产生驱动程序所需要的节点 ...

  9. Linux环境安装MQ

    MQ下载地址:http://www-03.ibm.com/software/products/us/en/wmq/ 安装的MQ软件包为WMQv600Trial-x86_linux_2.tar.gz.  ...

  10. AFHTTPClient的异步回调模式

    以前第一个版本,ios的http都用的同步模式,在很多地方会导致线程阻塞,自己开发了一个简易的AFHTTPClient的异步回调模式. 回调的protocol: @protocol MyAFNetwo ...