3.3 Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshold. Implement a data structure SetOf Stacks that mimics this. SetOf Stacks should be composed of several stacks and should create a new stack once the previous one exceeds capacity. SetOf Stacks. push() and SetOf Stacks. pop() should behave identically to a single stack (that is, pop() should return the same values as it would if there were just a single stack).
FOLLOW UP
Implement a function popAt(int index) which performs a pop operation on a specific sub-stack.

这道题让我们实现一个多个栈的数据结构,这个多个栈顾名思义是由许多子栈组成的,每个子栈都有相同的大小,在压入栈的过程中,如果当前子栈满了,则新建一个栈,压入新值。在移除栈顶元素时,若最上面的子栈移除后为空时,移除该子栈。还是返回栈顶元素和判断栈是否为空,这些基本的栈操作都不是很难,只要注意需要的时候新建和销毁子栈就行了。难点在于Follow Up,让我们来实现一个popAt函数,这个函数是要对于任意一个子函数来移除栈顶元素,找到并移除子栈的站定元素并不难,难点在于如果我们默认子栈必须都存满的话,那么如果中间的子栈栈顶被移除了,上面紧挨的子栈的栈底元素就要移到下面,成为下面的子栈的栈顶元素,然后以此类推,每个中间的子栈都要装满,这个实现起来就比较复杂,我们需要用递归来做,我们需要实现一个leftShift的辅助函数,这个函数可以移除子栈的栈顶元素或者栈底元素,具体实现参见代码如下:

class SetOfStacks {
public:
SetOfStacks(int capacity) : _capacity(capacity) {} void push(int x) {
stack<int> last;
if (!_stacks.empty() && _stacks.back().size() < _capacity) {
last = _stacks.back();
_stacks.pop_back();
}
last.push(x);
_stacks.push_back(last);
} void pop() {
if (!_stacks.empty()) {
stack<int> last = _stacks.back();
last.pop();
if (last.empty()) _stacks.pop_back();
}
} int top() {
if (!_stacks.empty()) {
stack<int> last = _stacks.back();
return last.top();
}
return ;
} bool empty() {
return _stacks.empty();
} void popAt(int index) {
leftShift(index, true);
} int leftShift(int index, bool removeTop) {
stack<int> *cur = &_stacks[index];
int removed_item;
if (removeTop) {
removed_item = cur->top();
cur->pop();
} else {
stack<int> tmp;
while (!cur->empty() && cur->size() != ) {
tmp.push(cur->top());
cur->pop();
}
removed_item = cur->top();
cur->pop();
while (!tmp.empty()) {
cur->push(tmp.top());
tmp.pop();
}
}
if (cur->empty()) _stacks.erase(_stacks.begin() + index);
else if (_stacks.size() > index + ) {
int val = leftShift(index + , false);
cur->push(val);
}
return removed_item;
} private:
vector<stack<int> > _stacks;
int _capacity;
};

[CareerCup] 3.3 Set of Stacks 多个栈的更多相关文章

  1. HDU 5818 Joint Stacks(联合栈)

    HDU 5818 Joint Stacks(联合栈) Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  2. [CareerCup] 3.1 Implement Three Stacks using Array 使用数组来实现三个栈

    3.1 Describe how you could use a single array to implement three stacks. 这道题让我们用一个数组来实现三个栈,书上给了两种方法, ...

  3. Implement Queue using Stacks(用栈实现队列)

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  4. Stacks of Flapjacks(栈)

     Stacks of Flapjacks  Background Stacks and Queues are often considered the bread and butter of data ...

  5. LeetCode 232 Implement Queue using Stacks 两个栈实现队列

    class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...

  6. UVA Stacks of Flapjacks 栈排序

    题意:给一个整数序列,输出每次反转的位置,输出0代表排序完成.给一个序列1 2 3 4 5,这5就是栈底,1是顶,底到顶的位置是从1~5,每次反转是指从左数第i个位置,将其及其左边所有的数字都反转,假 ...

  7. Careercup | Chapter 3

    3.1 Describe how you could use a single array to implement three stacks. Flexible Divisions的方案,当某个栈满 ...

  8. jvm是如何管理内存的

    1.JVM是如何管理内存的 Java中,内存管理是JVM自动进行的,无需人为干涉. 了解Java内存模型看这里:java内存模型是什么样的 了解jvm实例结构看这里:jvm实例的结构是什么样的 创建对 ...

  9. Java JVM、JNI、Native Function Interface、Create New Process Native Function API Analysis

    目录 . JAVA JVM . Java JNI: Java Native Interface . Java Create New Process Native Function API Analys ...

随机推荐

  1. iOS之UI--涂鸦画板实例 (有待更新)

    首先是搭建框架 其他的略过,直接展示效果: 然后接下来上传搭建好两个控制器框架的源码百度云下载链接: http://pan.baidu.com/s/1skjpDox 密码: ardx ,工程里面还有我 ...

  2. android 之 animations 动画

    android 提供的了两种机制你可以用来创建简单的动画:tweedned animation(渐变动画) 和 frame-by-frame animation(逐帧动画)(有道翻译的,汗!!!) . ...

  3. Volley源码分析(1)----Volley 队列

    Android网络框架很多,但是基于Google自己的volley,无疑是优秀的一款. 网络框架,无外乎解决一下几个问题,队列,缓存,图片异步加载,统一的网络请求和处理等. 一.Volley 队列 启 ...

  4. android的屏幕保持常亮

    1.Wake Lock是一种锁的机制 在Manifest.xml文件里面用user-permission声明.代码如下: 这种方法,在安装apk时,系统会提示安装人是否允许使用禁止休眠功能. < ...

  5. android network develop(3)----Xml Parser

    Normally, there are three type parser in android. Xmlpullparser, DOM & SAX. Google recomand Xmlp ...

  6. [Jmeter]打开jmeter.bat报错的解决思路与方法

    解决过程: 打开apache-jmeter-3.0的jmeter.bat时,报错如下: 查看报错信息,应该是属于环境变量配置问题. 因此加上jave_home的路径语句在jmeter.bat文件上: ...

  7. python数据结构-列表-基本操作

  8. 【php】mysql全局ID生成方案

    生产系统随着业务增长总会经历一个业务量由小变大的过程,可扩展性是考量数据库系统高可用性的一个重要指标;在单表/数据库数据量过大,更新量不断飙涨时,MySQL DBA往往会对业务系统提出sharding ...

  9. Java NIO入门

    NIO入门 前段时间在公司里处理一些大的数据,并对其进行分词.提取关键字等.虽说任务基本完成了(效果也不是特别好),对于Java还没入门的我来说前前后后花了2周的时间,我自己也是醉了.当然也有涉及到机 ...

  10. spring ioc DI 理解

    下面是我从网上找来的一些大牛对spring ioc和DI的理解,希望也能让你对Spring ioc和DI的设计思想有更进一步的认识. 一.分享Iteye的开涛对Ioc的精彩讲解 Ioc—Inversi ...