//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.

//使用一个链表来记录最小值的index
#include <iostream>
using namespace std;
#define MAXSIZE 100
#define INIT -888888 class mStack
{
public:
mStack()
{
top = -1;
mi = new min;
mi->data = 0;
mi->nextMin = NULL;
for (int i =0 ;i<100; i++)
{
data[i] = INIT;
}
} int pop()
{
if (top > -1)
{
if (top == mi->data)
{
min *t = mi;
mi = mi->nextMin;
delete t;
} int c= data[top];
data [top] = INIT;
top --;
return c;
}
else
return -2;
} bool push(int e)
{
if (top<MAXSIZE-1)
{
top++;
data[top] = e; if (data[mi->data] > data[top] && data[top]!= INIT)
{
min *t = new min;
t->data = top;
t->nextMin = mi;
mi = t;
}
return true;
}
else
{
return false;
}
} int getmin()
{
return data[mi->data];
} private:
int data[MAXSIZE];
int top; struct min
{
int data;
min *nextMin;
};
min *mi; }; int main()
{
mStack s; for (int i=0;i<10;i++)
{
s.push(10-i);
} s.pop();
s.pop();
cout<<s.getmin()<<endl;
return 0;
}

Cracking The Coding Interview 3.2的更多相关文章

  1. Cracking the coding interview

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

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

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

  3. Cracking the Coding Interview(Trees and Graphs)

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

  4. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

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

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

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

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

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

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

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

  10. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

随机推荐

  1. MySQL命令行的备份与还原

    备份的语法;  mysqldump -uroot -p shop> 数据库路径 还原的语法:  mysql -uroot -p 具体数据库(shop0609)< 数据库路径

  2. 关于sparksql操作hive,读取本地csv文件并以parquet的形式装入hive中

    说明:spark版本:2.2.0 hive版本:1.2.1 需求: 有本地csv格式的一个文件,格式为${当天日期}visit.txt,例如20180707visit.txt,现在需要将其通过spar ...

  3. gdb 不同位置,函数调用参数显示差异

    gdb 不同位置,函数调用参数显示差异,如: copy_strings (argc=1, argv=0xffcf08, page=0xffce6c, p=131068, from_kmem=2) at ...

  4. vue,vux判断字符串是否是undefined

    if (typeof thisObj.city === 'undefined') { return}

  5. 02 爬虫数据解析之re,xpath,beautifulsoup

    一.正则匹配 简单用法演示: 字符: print(re.findall(".","abccc31223dn哈哈")) ### . 匹配除了换行符以外的任意字符, ...

  6. array_unique

    array_unique() 函数移除数组中的重复的值,并返回结果数组. 当几个数组元素的值相等时,只保留第一个元素,其他的元素被删除. 返回的数组中键名不变. <?php $a=array(& ...

  7. xShell的简单操作记录

    查看java进程: ps ux | grep java 显示当前所有java进程pid的命令:jps 将压缩文件从本地上传到服务器上:rz 打包成jar包:mvn clean package -Dma ...

  8. Python中单线程、多线程和多进程的效率对比实验

    GIL机制导致如下结果: Python的多线程程序并不能利用多核CPU的优势 (比如一个使用了多个线程的计算密集型程序只会在一个单CPU上面运行)python多线程适合io操作密集型的任务(如sock ...

  9. Oracle 三大范式

    范式:数据库设计对数据的存储性能,还有开发人员对数据的操作都有莫大的关系.所以建立科学的,规范的的数据库是需要满足一些.规范的来优化数据数据存储方式.在关系型数据库中这些规范. 第一范式:数据库表中的 ...

  10. 牛客练习赛32-D-MST+tarjin割边

    链接:https://ac.nowcoder.com/acm/contest/272/D来源:牛客网 题目描述 小p和他的朋友约定好去游乐场游玩,但是他们到了游乐场后却互相找不到对方了. 游乐场可以看 ...