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. phpstorm 2017.1 激活

    打开网址 http://idea.lanyus.com/ 选择获取注册码,复制生成的验证码 安装完成后,打开软件,依次选择菜单栏 Help -> Register-> Activation ...

  2. 【转载】#437 - Access Interface Members through an Interface Variable

    Onece a class implementation a particular interface, you can interact with the members of the interf ...

  3. Uva 11582 巨大的斐波那契数 模运算

    题目链接:https://vjudge.net/contest/156903#problem/A 题意:计算 f(a^b)%n 分析: 1.斐波那契数列是 f(i+2) = f(i+1) + f(i) ...

  4. jQuery 遍历函数包括了用于筛选、查找和串联元素的方法。

    jQuery 参考手册 - 遍历 函数 描述 .add() 将元素添加到匹配元素的集合中. .andSelf() 把堆栈中之前的元素集添加到当前集合中. .children() 获得匹配元素集合中每个 ...

  5. 前端面试题(来自前端网http://www.qdfuns.com/notes/23515/c9163ddd620baac5dd23141d41982bb8.html)

    HTML&CSS 1. 常用那几种浏览器测试?有哪些内核(Layout Engine)? (Q1)浏览器:IE,Chrome,FireFox,Safari,Opera. (Q2)内核:Trid ...

  6. 删除已有的 HTML 元素

    如需删除 HTML 元素,您必须首先获得该元素的父元素: 实例 <div id="div1"> <p id="p1">这是一个段落.&l ...

  7. Hibernate 提供session的工具类HibernateUtils

    package cn.itcast.utils; import java.sql.Connection; import java.sql.SQLException; import org.hibern ...

  8. java异常处理 throw RuntimeException时不需要同时方法中声明抛出throws 异常等待调用者catch进行捕获 子父类异常问题

    package com.swift.exception1; public class Demo_Exception { public static void main(String[] args) { ...

  9. CBCGPImage的GetSize的问题及解决方法

    BCGControlBar Pro for MFC 25.10是目前(2018-07-16)网上能够找到的最新能够使用的版本,我配合Visual Studio 2010使用.在单文档MFC程序的视图中 ...

  10. Linux文件服务器实战(匿名用户)

    一.进程与线程 二.vsftp服务器 1.文件传输协议(file transfer protocol,FTP) 基于该协议ftp客户端和服务端实现文件共享,上传下载文件 FTP基于TCP协议生成一个虚 ...