Cracking The Coding Interview 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. //使用一个链表来记录最小值的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的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- python读写json文件(转)
https://www.cnblogs.com/bigberg/p/6430095.html 利用python中的json库处理数据(包含json的四种方法:dumps.dump.loads.load ...
- Redis的安装及命令返回值
Linux下安装Reids : http://redis.io/download 下载最新稳定版本 wget http://download.redis.io/releases/redis-3.0.7 ...
- (转)winform之ListView
一.ListView类 1.常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. (2)GridLines:设置 ...
- C# 测试代码段性能耗时
一: DateTime BeginTime = System.DateTime.Now; //代码 DateTime EndTi ...
- 20180429 xlVBA套打单据批量复制
Sub testCopyModelRange() Set ModelSheet = ThisWorkbook.Worksheets("单据模板") Set PrintSheet = ...
- apply、call
call(),apply() 1.每个函数都包含两个非继承而来的方法:call()和apply() 2.在特定的作用域内调用函数,等于设置函数体内的this对象,以扩充函数赖以运行的作用域 3.app ...
- 使用VisualStudio进行脚本|样式文件压缩
在vs的Optimization中有个Bundle是专门用来压缩样式和脚本文件 .他有两个继承:StyleBundle.ScriptBundle,从名字上就可看出,StyleBundle专门压缩样式文 ...
- pytorch 中的 split
Pytorch中的split问题: 1.使用torch.nn.Conv2d中有个参数是groups会将输入的feature map分组,此处需要注意的一点是分组之后各组的feature map的cha ...
- php 常用设计模式demo
<?php//__get()//__set()当对象中属性不存在时调用该魔术方法//__call()当对象中方法不存在时//__callStatic()静态方法//__string()当对象不能 ...
- 内存池、进程池、线程池介绍及线程池C++实现
本文转载于:https://blog.csdn.net/ywcpig/article/details/52557080 内存池 平常我们使用new.malloc在堆区申请一块内存,但由于每次申请的内存 ...