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. JQuery怎么实现页面刷新后保留鼠标点击样式的方法

    今天抽空研究了下鼠标点击添加样式的方法.为了防止忘记,写篇文章算是备份吧. $('ul.main-menu li a').each(function(){     if($($(this))[0].h ...

  2. C++ 构造函数中调用虚函数

    我们知道:C++中的多态使得可以根据对象的真实类型(动态类型)调用不同的虚函数.这种调用都是对象已经构建完成的情况.那如果在构造函数中调用虚函数,会怎么样呢? 有这么一段代码: class A { p ...

  3. C++中const用法总结

    1修饰变量/指针 注意以下几种修饰的区别: (1)const int * a; (2)int const *a; (3)int * const b; (4)int const* const c; 其中 ...

  4. SQL 在表中随机取数据

    在一张10万行产品表(Product)中,随机取10条数据的几种方式: SET STATISTICS IO ON SELECT TOP 10 ID FROM dbo.Product(NOLOCK) W ...

  5. selinux开启关闭

    查看SELinux状态: 1./usr/sbin/sestatus -v      ##如果SELinux status参数为enabled即为开启状态 SELinux status:         ...

  6. celery 快速入门教程 celery 定时器

    当然首先得安装celery和rabbitmq-server,如果有redis需要安装redis 安装Redis $ yum install redis 启动 Redis $redis-server 检 ...

  7. Oracle日期周详解IW

    1 ORACLE中周相关知识描述 1.1           日期格式化函数 TO_CHAR(X [,FORMAT]):将X按FORMAT格式转换成字符串.X是一个日期,FORMAT是一个规定了X采用 ...

  8. java使用this关键字调用本类重载构造器

    在构造器中可以调用本类的其他重载构造器,不能使用构造器名称来调用另一个构造器,而是应该使用Java特定的this(-.)来调用. this(-.)方法必须出现在构造器中的第一行,用来调用其他重载构造器 ...

  9. 无法将匿名方法转换为System.Delegate

    在WinForm中,不允许非UI线程访问UI,如果非UI线程需要跨线程调用UI控件,通常的解决办法是使用Control类中的Invoke方法,传递给该方法一个委托和委托调用的参数列表(params [ ...

  10. hive的常用命令

    #从hive中直接进入hdfs的daas/bstl/term/userinfo目录下 hive> !hadoop fs -ls /daas/bstl/term/userinfo; 查看hive表 ...