2014-04-24 00:05

题目:用拉链法设计一个哈希表。

解法:一个简单的哈希表,就看成一个数组就好了,每个元素是一个桶,用来放入元素。当有多个元素落入同一个桶的时候,就用链表把它们连起来。由元素值到哈希值的映射就是哈希函数了。

代码:

 // 8.10 Design a hash table. Handle conflicts with chaining(linked lists).
#include <iostream>
#include <string>
#include <vector>
using namespace std; class HashMap {
public:
HashMap() {
_buckets.resize(_bucket_num);
int i; for (i = ; i < _bucket_num; ++i) {
_buckets[i] = nullptr;
}
}; bool contains(int key) {
key = (key > ) ? key : -key;
key = key % _bucket_num;
LinkedList *ptr = _buckets[key]; while (ptr != nullptr) {
if (ptr->key == key) {
return true;
}
} return false;
}; int& operator [] (int key) {
key = (key > ) ? key : -key;
key = key % _bucket_num;
LinkedList *ptr = _buckets[key]; if (ptr == nullptr) {
_buckets[key] = new LinkedList(key);
return _buckets[key]->val;
} LinkedList *ptr2 = ptr->next;
if (ptr->key == key) {
return ptr->val;
} while (ptr2 != nullptr) {
if (ptr2->key == key) {
return ptr2->val;
} else {
ptr = ptr->next;
ptr2 = ptr2->next;
}
}
ptr->next = new LinkedList(key);
ptr = ptr->next;
return ptr->val;
} void erase(int key) {
key = (key > ) ? key : -key;
key = key % _bucket_num;
LinkedList *ptr = _buckets[key]; if (ptr == nullptr) {
return;
} else if (ptr->next == nullptr) {
if (ptr->key == key) {
delete _buckets[key];
_buckets[key] = nullptr;
}
return;
} if (ptr->key == key) {
_buckets[key] = ptr->next;
delete ptr;
return;
} LinkedList *ptr2;
ptr2 = ptr->next; while (ptr2 != nullptr) {
if (ptr2->key == key) {
ptr->next = ptr2->next;
delete ptr2;
return;
} else {
ptr = ptr->next;
ptr2 = ptr2->next;
}
}
} ~HashMap() {
int i;
LinkedList *ptr; for (i = ; i < _bucket_num; ++i) {
ptr = _buckets[i];
while (ptr != nullptr) {
ptr = ptr->next;
delete _buckets[i];
_buckets[i] = ptr;
}
}
_buckets.clear();
}
private:
struct LinkedList {
int key;
int val;
LinkedList *next;
LinkedList(int _key = , int _val = ): key(_key), val(_val), next(nullptr) {};
}; static const int _bucket_num = ;
vector<LinkedList *> _buckets;
}; int main()
{
HashMap hm;
string cmd;
int op1, op2; while (cin >> cmd) {
if (cmd == "set") {
cin >> op1 >> op2;
hm[op1] = op2;
} else if (cmd == "get") {
cin >> op1;
cout << hm[op1] << endl;
} else if (cmd == "find") {
cin >> op1;
cout << (hm.contains(op1) ? "true" : "false") << endl;
}
} return ;
}

《Cracking the Coding Interview》——第8章:面向对象设计——题目10的更多相关文章

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

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

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

  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》——第8章:面向对象设计——题目9

    2014-04-23 23:57 题目:如何设计一个内存文件系统,如果可以的话,附上一些代码示例. 解法:很遗憾,对我来说不可以.完全没有相关经验,所以实在无从入手.这题目应该和工作经验相关吧? 代码 ...

  8. 《Cracking the Coding Interview》——第8章:面向对象设计——题目8

    2014-04-23 23:49 题目:有个棋牌游戏叫Othello,也叫Reversi.请看游戏规则.中文应该叫黑白棋吧,不常玩儿就是了. 解法:既然这题的规则很清楚,也很清楚,我就写了一个命令行的 ...

  9. 《Cracking the Coding Interview》——第8章:面向对象设计——题目7

    2014-04-23 23:38 题目:你要如何设计一个聊天服务器,有什么技术难点? 解法:这是基于工作经验的面试题吗?否则,一个new grad碰上这种题目能打点草稿也就算不错了. 代码: // 8 ...

随机推荐

  1. vuejs非父子组件传值

    当父组件要给孙子,或者孙子与孙子要传值的时候怎么传,一层一层传太麻烦了,vuejs提供了一中模式叫发布订阅模式(观察者模式,bus,总线)来处理非父子组件间的传值 <div id='root'& ...

  2. ORA-01262,oracle启动报错,及Oracle启动原理

    错误状态: SQL> startup ORA-01261: Parameter db_recovery_file_dest destination string cannot be transl ...

  3. grep的使用

    http://www.eguidedog.net/linux-tutorial/05-grep.php grep apple fruitlist.txt:在fruitlist.txt中查找apple字 ...

  4. Mybatis-generator自动生成

    第一步:导入架包 <build> <plugins> <plugin> <groupId>org.mybatis.generator</group ...

  5. iOS第三方开放者平台概览

    前言:记录一些可能用到过的第三方开放者平台相关内容 视频类: 腾讯云移动直播:https://cloud.tencent.com/product/mlvb 遇到问题后发起工单是一种比较好的解决问题的方 ...

  6. JAVA通用BaseServlet的产生和代码实现

    BaseServlet的作用: 我们先写一个工具类:BaseServlet. 我们知道,写一个项目可能会出现N多个Servlet,而且一般一个Servlet只有一个方法(doGet或doPost),如 ...

  7. udp发送广播消息

    import socket if __name__ == '__main__': # 创建udpsocket udp_socket = socket.socket(socket.AF_INET, so ...

  8. 用 js 写一个获取随机颜色的程序

    function getColor(){ var color="#"; for(var i=0;i<6;i++){ color+=(Math.random()*16 | 0) ...

  9. (二)活用ComponentScan

    项目改造成spring cloud项目后,有非常多组件是复用的,比如(一)敏感信息混淆的组件,比如数据库.Redis等配置, 比如常用的api组件Swagger配置.每个微服务组件里都会有若干个组件随 ...

  10. http网络协议 学习摘要

    一:HTTP协议状态码 状态码主要用于描述当客户端向服务器发送请求时的返回结果,标记服务端的处理是否正常,通知出现的错误等工作. 状态码整体分为五大类: 1开头的状态码:信息类状态码,主要接收请求,表 ...