《Cracking the Coding Interview》——第13章:C和C++——题目2
2014-04-25 19:29
题目:对比一下哈希表和STL中的map的区别,哈希表如何实现?如果数据规模比较小,可以用什么来代替哈希表?
解法:哈希表可以理解为一堆桶,每个桶都有唯一的id,桶里可以存至少一个元素;而STL的map是一棵平衡二叉搜索树,每个节点存一个元素。还有很多细节要说,如果on-site面试的话,也许可以写写画画,或者直接写出一个简单的哈希表来,参考unordered_map。如果数据规模小的话,直接用数组就可以了。之前有一道题让实现一个哈希表,所以在这儿就不重复写一次了。
代码:
// 13.2 Expain your understanding on hash table and STL map. If data scale is small, what can be used to replace hash table?
// Answer:
// Hash table is a data structure which holds item in buckets. Every bucket has a hash number, which is computed by a hashFunction().
// When inserting or searching in the hash table, the key is passed to the hashFunction() to calculate the corresponding hash number, that will decide which bucket is to hold this item.
// While there will be multiple elements with the same hash number, known as collision, the hash table has to be equipped with probing strategy, which decides where the next proper position to hold the item is.
// Three important properties about hash table:
// 1. == operator
// 2. hashFunction()
// 3. probing strategy, such as linear, quadratic, polynomial, chaining and so on
// A single operation could reach O(1) time, but collision will require extra probing time.
//
// STL map is a red-black tree, which is a high-balanced binary search tree.
// The elements in STL map is sorted, as the nodes in a BST is inserted based on comparison.
// A single operation could reach O(log(n)) time.
// Two important properties aboutt STL map:
// 1. < operator
// 2. red-black tree
int main()
{
return ;
}
《Cracking the Coding Interview》——第13章:C和C++——题目2的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《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
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- 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》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第17章:普通题——题目13
2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目10
2014-04-25 20:47 题目:分配一个二维数组,尽量减少malloc和free的使用次数,要求能用a[i][j]的方式访问数据. 解法:有篇文章讲了六种new delete二维数组的方式,其 ...
随机推荐
- "COM Surrogate 已停止工作"解决方案(windows7 64位及32位)
根据图示步骤,将以下文件添加至“数据执行保护”的例外列表中. 64位:C:Windows\SysWOW64\dllhost.exe 32位:C:\Windows\System32\dllhost.ex ...
- 119. Pascal's Triangle II (Amazon) from leetcode
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- 指定类型的成员XX”不支持实体LINQ。只有初始化,成员单位,和实体导航性能的支持。
The specified type member 'DeleteFlag' is not supported in LINQ to Entities. Only initializers, enti ...
- http知识补充
在我的职业生涯中,没怎么重视过这http四个字,想当然的觉得不就是个网页请求嘛就没怎么当回事,而且很多http相关的长篇大论一听就困,真心是弄不下去,但是就是这种观念导致我后期的工作中不断的挖坑,不断 ...
- Python IDE PyCharm的快捷键大全
Python IDE PyCharm的快捷键大全 1.编辑(Editing) Ctrl + Space 基本的代码完成(类.方法.属性) Ctrl + Alt + Space 快速导入任意类 Ctrl ...
- N76E003---输入捕获
输入捕获 根据芯片手册,定时器2可以作为输入捕获使用,设置非常简单,官方也提供了宏给我们使用 void Time2_cap_init(void) { /******* 输入捕获CF设置 ******* ...
- Miller rabin
蛤蛤,终于基本上搞懂了 #include<iostream> #include<cstdio> using namespace std; long long num[10]={ ...
- 你可能不知道的 new.target
new 是构造函数生成实例的命令, ES6为 new 命令引入了 new.target属性.这个属性用于确定构造函数是怎么调用的. 在构造函数中, 如果一个构造函数不是通过 new操作符调用的, ne ...
- v-show
v-show的原理是当值为false的时候,元素display:none 隐藏了元素且脱离文档流,但是在dom 中仍然存在. 与v-if使用场景不同,文档中提到,当需要高频切换的时候使用v-show ...
- 本地预览的vue项目,在githubpage静态展示
本地项目github静态展示 前提 在本地npm run dev后能够在本地端口正常显示 githubpage为自己的静态页面 上线 config/index.js中设置assetsPublicPat ...