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的更多相关文章

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

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

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

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

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

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

  4. Cracking the coding interview

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

  5. Cracking the Coding Interview(Trees and Graphs)

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

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

  7. 《Cracking the Coding Interview》——第18章:难题——题目13

    2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...

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

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

  9. 《Cracking the Coding Interview》——第17章:普通题——题目13

    2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...

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

    2014-04-25 20:47 题目:分配一个二维数组,尽量减少malloc和free的使用次数,要求能用a[i][j]的方式访问数据. 解法:有篇文章讲了六种new delete二维数组的方式,其 ...

随机推荐

  1. PHP : 封装跳转函数,实现三个页面的跳转

    具体实现:有a,b两个页面,一个跳转页面c,在a执行完后先进行c页面的提示,再跳转到b 1.文件设计: 2.c页面封装方法内容(function.php): a页面内容(a.html): a页面的后台 ...

  2. 模拟网页的浏览Stack(POJ1028)

    题目链接:http://poj.org/problem?id=1028 注意: 1.用两个栈来模拟,一个用来存可以返回的,一个用来存可以前进的. 2.visit方法,就要将可以前进的栈清空. 3.ba ...

  3. 编译安装 mysql 5.5,运行 cmake报错Curses library not found

    是因为 curses库没有安装,执行下面的语句即可 yum -y install ncurses-devel 如果上述命令的结果是no package,则使用下面的命令安装 apt-get insta ...

  4. Node.js 的初体验

    例子1: 1.首先第一步 :要 下载 node.js. 官网 上可以下载 下载完后,是这个玩意. 2. 打开 node.js ,然后输入 // 引入http模块 var http = require( ...

  5. BCB:如何在BCB中使用CodeGuard

    www.educity.cn 发布者:xjxyj2006 来源:网络转载 发布日期:2013年12月13日 文章评论 发表文章 一. 为什么写这篇东西 自己在使用 BCB5 写一些程序时需要检查很多东 ...

  6. 永久免费开源的卫星地形图地图下载工具更新Somap2.13版本功能更新 更新时间2019年2月22日13:59:05

    一.下载地址 最新版本下载地址:SoMap2.13点击此处下载  二.系统自主开发特色功能展示 1.上百种地图随意下载 高德.百度.arcgis.谷歌.bing.海图.腾讯.Openstreet.天地 ...

  7. 【赛时总结】 ◇赛时·II◇ AtCoder ABC-100

    ◆赛时·II◆ ABC-100 ■唠叨■ ABC终于超过百场比赛啦(毫不犹豫地参加).然后莫名其妙的好像是人很多,评测慢得不可理喻.然后我就--交了一大发--错误程序--然后B题就没了.最后的D题居然 ...

  8. lintcode_115_不同的路径 II

    不同的路径 II   描述 笔记 数据 评测 "不同的路径" 的跟进问题: 现在考虑网格中有障碍物,那样将会有多少条不同的路径? 网格中的障碍和空位置分别用 1 和 0 来表示. ...

  9. OpenStack Grizzly版本部署(离线)

    参考原版地址:https://github.com/mseknibilel/OpenStack-Grizzly-Install-Guide 图转自原版地址 部署拓扑: 部署环境:vmware stat ...

  10. scrapy--Cookies

    大家好,之前看到的关于cookies的应用,由于有段时间没看,再看的时候花了一些时间,来给大家总结下.本文是根据:"http://www.bubuko.com/infodetail-2233 ...