Cracking the Coding Interview(Stacks and Queues)

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. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  2. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  3. Cracking the Coding Interview(linked list)

    第二章的内容主要是关于链表的一些问题. 基础代码: class LinkNode { public: int linknum; LinkNode *next; int isvisit; protect ...

  4. Cracking the Coding Interview(String and array)

    1.1实现一个算法判断一个字符串是否存在重复字符.如果不能利用另外的数据结构又该如何实现? My solution: /** *利用类似一个hash table的计数 *然后检查这个hash tabl ...

  5. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  6. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  7. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  8. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  9. Cracking the Coding Interview 150题(二)

    3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...

随机推荐

  1. mac github工具将命令当下来的代码拖入macgithub中就可以

    mac github工具将命令当下来的代码拖入macgithub中就可以,刚開始傻傻的就知道点击那个加入button,总是在当下来的文件夹下创建个文件夹.并且代码不能同步

  2. css优先级计算规则

    原文:css优先级计算规则 最近面试了一些求职者,我问css优先级计算规则是怎样的?答曰ID优先级>class>元素选择器,外联样式优先级低于内联样式,内联样式优先级低于行间样式,然后就没 ...

  3. 位记录——Windows 7已安装Sublime Text 3、cynwin、SublimeClang

    转载请注明出处:http://blog.csdn.net/cywosp/article/details/34429697 1. 到https://www.cygwin.com/下载setup-x86_ ...

  4. 解决tomcat占用8080端口

    怎么解决tomcat占用8080端口问题图文教程           怎么解决tomcat占用8080端口问题 相信很多朋友都遇到过这样的问题吧,tomcat死机了,重启eclipse之后,发现 Se ...

  5. C#操作 Advantage Database Server 数据库

    相关下载 http://devzone.advantagedatabase.com/dz/content.aspx?key=31 1.安装数据库: Advantage Database Server ...

  6. 微信应用号开发知识贮备之Webpack实战

    天地会珠海分舵注:随着微信应用号的呼之欲出,相信新一轮的APP变革即将发生.作为行业内人士,我们很应该去拥抱这个趋势.这段时间在忙完工作之余准备储备一下这方面的知识点,以免将来被微信应用号的浪潮所淹没 ...

  7. c#万能视频播放器(附代码)

    原文:c#万能视频播放器(附代码) c#万能视频播放器 本人之前很多的文章中均提到了使用libvlc为播放器内核制作的播放器,也许有些朋友对此感兴趣,于是我用c#写了一个调用libvlc api实现的 ...

  8. 有趣的游戏:Google XSS Game

    Google最近出了一XSS游戏: https://xss-game.appspot.com/ 我这个菜鸟看提示,花了两三个小时才全过了.. 这个游戏的规则是仅仅要在攻击网页上弹出alert窗体就能够 ...

  9. 多线程下HashMap的死循环是如何产生的

    前言 HashMap不是线程安全的,如果需要在多线程环境中使用Map,那么我们可以使用ConcurrentHashmap. 1.举例说明: package com.test; import java. ...

  10. leetcode[67] Plus One

    题目:对一个用vector存的数字进行加1,然后返回加1后的值. 一次就在oj上通过了. 就是进位加上当前位如果大于9,那就当前位等于0: 随后进位还为1的话就是在数组前面插入一个1: class S ...