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

我的思路:一般堆栈的实现会利用一个数组,这里一个数组若实现3个堆栈,直接考虑把数组划分为3个部分,相当于3个独立的数组,所以就有以下的实现。

但是,这种实现方式的缺点在于均分了每个stack需要的space,但是事先无法确定每个stack是否需要更多的space相比于其他的stack。但是针对这个缺陷我没有想到解决的方案。但是书中提供了一种解决思路。

class stack_share_array
{
public:int top;int gap;stack_share_array(int initnum,int gap_dis):top(initnum),gap(gap_dis){}void push(int num){array[top] = num;top += gap;}int pop(){top -= gap;int result = array[top];return result;}static int array[15];
};

分成3个使用方法如下使用:

        stack_share_array* stackthree[3];for (int i = 0;i < 3;i++){stackthree[i] = new stack_share_array(i,3);}int test1 = 10;int test2 = 20;int test3 = 30;stackthree[0]->push(test1++);stackthree[0]->push(test1++);stackthree[0]->push(test1++);stackthree[0]->push(test1++);stackthree[0]->push(test1++);stackthree[1]->push(test2++);stackthree[1]->push(test2++);stackthree[1]->push(test2++);stackthree[1]->push(test2++);stackthree[1]->push(test2++);stackthree[2]->push(test3++);stackthree[2]->push(test3++);stackthree[2]->push(test3++);stackthree[2]->push(test3++);stackthree[2]->push(test3++);

3种划分方式多种多样,不限于上限的实现。也可以不等分的划分。

另一种思路:一种在数组里面形成链表的思路,及每个数组单元重新定义,除了保存堆栈的存储信息之外保存前一个元素的信息,方便堆栈顶部的向前移动。这样就能够比较自由的分配3个堆栈所占数组的空间。

2.How would you design a stack which,in addition to push and pop,also has a function min which returns the minimum element?Push,pop and min should all operate in O(1) time.

我的思路:

因为min要能够在O(1)能够返回结果,所以min的操作必定之前保存的应该有记录。我第一直觉是用一个int保存最小值,但是细细想一下,如果pop()之后无法确保这个最小值是否被pop(),如果pop()了,无法更新余下的最小值,思考了很久之后发现可以保存一个根据当前top值来返回min的min数组,从开始堆栈内容为0的时候进行记录。所以这个数组的长度应该与设计堆栈的数组的长度相同。

class stack_min
{
public:int array[15];int min_array[16];int top;stack_min():top(0){min_array[0] = BIG;}void push(int num){array[top] = num;top ++;if (num < min_array[top - 1]){min_array[top] = num;}else{min_array[top] = min_array[top - 1];}}int pop(){top --;int result = array[top];return result;}int min(){return min_array[top];}
};

这样空间复杂度有些高,书中提供另外一种能够节约空间复杂度的算法。

思路是将min数组设计为一个堆栈,这样就能够不需要重复保存很多冗余的最小值。因为利用数组会重复保存同样的最小值多次。

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 SetOfStacks that mimics this.SetOfStacks should be composed of several stacks, and should create a new stack once the previous one exceeds capacity.SetOfStacks.push() and SetOfStacks.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.

class Stack
{
public:int top;int array[10];Stack():top(0){}void push(int num){array[top] = num;top ++;}int pop(){top --;int result = array[top];return result;}
};
class setofstacks
{
public:Stack* stackArray[10];int x;setofstacks():x(0){for (int i = 0;i < 10;i++){stackArray[i] = new Stack();}}void push(int num){if (stackArray[x]->top < 10){stackArray[x]->push(num);}else{x++;push(num);}}int pop(){if (stackArray[x]->top > 0){return stackArray[x]->pop();}else{x--;return pop();}}int popAt(int index){return stackArray[index]->pop();}
};

上面我的实现有这些问题。

首先,数组长度固定,如果最后一个堆栈用完,无法扩展数组长度。

其次,popAt(index)之后也许需要把后面的元素向前挪动,不然就会出现类似内存碎片一样的无法利用的空间。

4.Write a program to move the disks from the first rod to the last using Stacks.

关于汉诺塔的问题一直都是递归的经典问题,但是一般都是计算时间复杂度的举例,利用堆栈模拟这个过程还真不太会...

模拟的过程实习也是一个实际问题转换为编程问题,需要进行一些抽象化。

class Tower
{
public://stack<int>* disks;int num;Tower(int i):num(i){disks = new stack<int>();}int index(){return num;}void add(int d){if (!disks->empty() && disks->top() <= d){return;}else{disks->push(d);}}void movetopto(Tower* t){int top = disks->top();disks->pop();t->add(top);cout<<"Move disk"<<top<<"from"<<index()<<"to"<<t->index()<<endl;}void moveDisks(int n,Tower* dst,Tower* buffer){if (n>0){moveDisks(n-1,buffer,dst);movetopto(dst);buffer->moveDisks(n-1,dst,this);}}
};

5.Implement a MyQueue class which implements a queue using two stacks.

class queue
{
public:Stack* stacktwo[2];queue(){for (int i = 0;i < 2;i++){stacktwo[i] = new Stack();}}void push(int num){stacktwo[0]->push(num);}int pop(){if (stacktwo[1]->top == 0){while (stacktwo[0]->top != 0){stacktwo[1]->push(stacktwo[0]->pop());}}return stacktwo[1]->pop();}
};

一个堆栈存放push()进来的元素,pop()的时候直接pop()另外一个堆栈,如果空了则将第一个堆栈中的元素添加进来,再执行pop()操作,这样两次之后就是先进先出的队列。

6.Write a program to sort a stack in ascending order.You should not make any assumptions about how the stack is implemented.The following are the only functions that should be used to write this program:push|pop|peek|isEmpty.

copy code from the book:

public static Stack<Interger> sort(Stack<Integer> s)
{Stack<Integer> r = new Stack<Integer>();while(!s.isEmpty()){int tmp = s.pop();while(!r.isEmpty() && r.peek() > tmp)s.push(r.pop());r.push(tmp)}return r;
}

Cracking the Coding Interview(Stacks and Queues)相关推荐

  1. 渣基础:比照Hawstein学Cracking the coding interview(1)

    <C++ Primer 第五版>书实在是太长,太厚了.总是看了十几页就看累了,坚持不了多久,想了想还是别勉强自己,决定把它当工具书查看,或者积累足够的C++经验后再翻阅一遍. 目前的打算是 ...

  2. 渣基础:比照Hawstein学Cracking the coding interview(4)

    作者:Hawstein 出处: http://hawstein.com/posts/2.1.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commo ...

  3. 渣基础:比照Hawstein学Cracking the coding interview(3)

    作者:Hawstein 出处:http://hawstein.com/posts/1.4.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Common ...

  4. 渣基础:比照Hawstein学Cracking the coding interview(2)

    作者:Hawstein 出处:http://hawstein.com/posts/1.4.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Common ...

  5. Cracking the coding interview

    转自:http://hawstein.com/posts/ctci-solutions-contents.html Cracking the coding interview--问题与解答 March ...

  6. 经典算法题目:Cracking the coding interview 问题与解答

    Cracking the coding interview--问题与解答 March 14, 2013 作者:Hawstein 出处: http://hawstein.com/posts/ctci-s ...

  7. [Free] Cracking the Coding Interview 6th Download

    [Free] Cracking the Coding Interview 6th Download 推荐给有梯子的童鞋们! https://www.pdfdrive.com/cracking-the- ...

  8. Google面试之快乐智力题(Cracking the Coding Interview)

    问:有20瓶药,其中19瓶装的都是1.0克的药片,只有1瓶装了1.1克的药.给你一个能称出具体克数的电子秤,只允许你称一次,怎么找出那瓶不一样的? 答:如果药片足够,从第i(1<i<20) ...

  9. Interview Questions: Stacks and Queues

    Stacks and Queues Queue with two stacks. Implement a queue with two stacks so that each queue operat ...

最新文章

  1. 未能加载类型“URLRewriter.ModuleRewriter”。 解决方法
  2. 聊聊flink的CsvTableSink
  3. 组件注册——@ComponentScan自动扫描组件指定扫描规则
  4. 吴恩达《Machine Learning》精炼笔记 4:神经网络基础
  5. multipathd: sdn: readsector0 checker reports path is down 多路径出错问题解决
  6. VC++ 删除当前读取行 代码
  7. 干货整理:处理不平衡数据的技巧总结!收好不谢
  8. 二分查找(划分时左右元素个数不相等)解析+代码
  9. 计算机启动进入不了桌面图标,电脑开机后不显示桌面图标如何通过修改注册表解决问题...
  10. html页面关闭执行函数,html页面调用js文件里的函数报错--方法名 is not defined处理方法...
  11. 第三十一篇 玩转数据结构——并查集(Union Find)
  12. Python初学手记----在window系统中安装环境
  13. BZOJ4477: [Jsoi2015]字符串树
  14. 西门子的十一位CEO
  15. 最大子列和问题(PTA)
  16. 电子产品做3C认证检测标准是什么
  17. Photoshop —— 白色(或任意颜色)或黑色物体修改成任意色彩
  18. ACM常用的解题技巧:尺取法
  19. Maven的生命周期和插件
  20. Ipad上选择专业好用的思维导图软件

热门文章

  1. css3 position: fixed 居中问题,移动端,旁边留白的情况fixed不能居中的问题;
  2. vuex commit 传参数,传递多个参数
  3. HTML二刺螈网址导航模板
  4. 织梦手机软件应用app下载排行网站模板
  5. 说说过游戏保护(4)
  6. Samba 服务器的构建
  7. 双语经典:告别单身的必杀技之情话连篇
  8. 每个Linux用户都应该了解的命令行省时技巧
  9. 腾讯图片处理 Tencent AlloyTeam 2013
  10. 深入理解Magento – 第三章 – 布局,块和模板