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. 【转】C内存操作函数

    一.malloc/calloc 名称: Malloc/calloc 功能:  动态内存分配函数 头文件: #include <stdlib.h> 函数原形: void *malloc(si ...

  2. MHA启动及关闭

    MHA启动及关闭 #masterha_manager --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/ap ...

  3. Gym - 100004A 树的性质

    题目: 题意: 从节点 0 出发,把每一个节点都经过一遍,然后从一个节点回到学校. 由于有 n+1个节点,n条边,而且保证两两互相到达,那么这就是一个棵树. 于是,可以发现,如果从一个点出发,然后回到 ...

  4. Linux(二) - Unix&Linux 基本概念

    主机 = 内核 + 实用工具 内核(kernel) 当计算机启动时,计算机要经历一系列动作,这些动作构成了引导过程.引导过程的最后一个动作是启动一个非常复杂的程序,该程序就被称为内核(Kernel) ...

  5. Logback初始化失败问题排查(Web.xml中context-param配置详解)

    监控部分反馈异常,生产系统日志文件竟然木有了(最后一次版本发布后,再也无日志文件生成). 问题排查步骤: 1. 检查logback配置文件 日志生成目录一切正常 应该服务器上磁盘空间未满.操作权限没有 ...

  6. python main

    python中的main函数,总体来说就是,main比较适合写test测试,有点类似于java中的testcase,就是程序单独运行时是运行main的,但是当被调用时就不会运行main了.具体可以参考 ...

  7. 内置函数SQLCODE和SQLERRM的使用

    由于ORACLE的错信息最大长度是512字节,为了得到完整的错误提示信息,我们可用 SQLERRM 和 SUBSTR 函数一起得到错误提示信息,方便进行错误,特别是如果WHEN OTHERS异常处理器 ...

  8. BFS算法入门--POJ3984

    迷宫问题–POJ3984 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22008 Accepted: 12848 Descri ...

  9. harbor 配置 Keepalived 实现HA

    环境说明: $ cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) vip : 192 168 93 142 MASTER-har ...

  10. ssm整合-图片上传功能(转)

    本文介绍 ssm (Spring+SpringMVC+Mybatis)实现上传功能. 以一个添加用户的案例介绍(主要是将上传文件). 一.需求介绍 我们要实现添加用户的时候上传图片(其实任何文件都可以 ...