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. PHP错误日志控制(display_errors和error_reporting)

    display_errors和error_reporting是php程序调试过程中两个非常重要的参数,下面就来介绍一下这两个错误日志的配置如何开启和关闭: 我们知道在产品的生产环境肯定是不能够显示错误 ...

  2. 如何在Java Filter 中注入 Service

    在项目中遇到一个问题,在 Filter中注入 Serivce失败,注入的service始终为null.如下所示: public class WeiXinFilter implements Filter ...

  3. JMS介绍入门大白话版

    以下内容转自: http://setting.iteye.com/blog/1097767 ------------------------------------------------------ ...

  4. hadoop入门:hadoop使用shell命令总结

    第一部分:Hadoop Bin后面根据项目的实际需要Hadoop Bin  包括:Hadoop  hadoop的Shellhadoop-config.sh 它的作用是对一些变量进行赋值     HAD ...

  5. [转载]两个半小时学会Perl

    Learn Perl in about 2 hours 30 minutes By Sam Hughes Perl is a dynamic, dynamically-typed, high-leve ...

  6. uva 1152 4 values whose sum is zero ——yhx

    The SUM problem can be formulated as follows: given four lists A;B;C;D of integer values, computehow ...

  7. C之五子棋

    #include <stdio.h> #include <stdlib.h> #define N 15 ][N + ] = { }; ; void initGame(void) ...

  8. 软件工程实训项目案例--Android移动应用开发

    实训过程 角色分工 1.项目经理:负责项目的组织实施,制定项目计划,并进行跟踪管理 2.开发人员:对项目经理及项目负责 3.需求分析员:负责系统的需求获取和分析,并协助设计人员进行系统设计 4.系统设 ...

  9. UESTC 912 树上的距离 --LCA+RMQ+树状数组

    1.易知,树上两点的距离dis[u][v] = D[u]+D[v]-2*D[lca(u,v)] (D为节点到根节点的距离) 2.某条边<u,v>权值一旦改变,将会影响所有以v为根的子树上的 ...

  10. Spring AOP 注解和xml实现 --转载

    AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...