[CareerCup] 8.10 Implement a Hash Table 实现一个哈希表

 

8.10 Design and implement a hash table which uses chaining (linked lists) to handle collisions.

 

这道题让我们实现一个简单的哈希表,我们采用了最简单的那种取余映射的方式来实现,我们使用Cell来保存一对对的key和value的映射关系,然后每一个格子都用一个list链表来保存所有的余数为该格子序号的Cell,我们设定格子总数为10,然后我们用泛式编程来适用于所有的参数类型,然后实现哈希表的基本存数和取数的功能。现在大多数的哈希表都是用二叉搜索树来实现的,但是用BST的话取数就是不是O(1)的时间复杂度了(如果我们以后很多的collision的话),但是BST的好处就是省空间,不需要建立超大的数组来保存映射。

 

template<typename K, typename V>
class Cell{
public:Cell(K k, V v): _key(k), _value(v) {}bool equivalent(Cell *c) {return equivalent(c->getKey());}bool equivalent(K k) {return _key == k;}K getKey() { return _key; }V getValue() { return _value; }private:K _key;V _value;
};template<typename K, typename V>
class Hash {
public:Hash() {_items.resize(_MAX_SIZE);}int hashCodeOfKey(K key) {return sizeof(key).size() % _items.size();}void put(K key, V value) {int x = hashCodeOfKey(key);if (_items[x] == nullptr) {_items[x] = new list<Cell<K, V>*> ();}list<Cell<K, V>*> *collided = _items[x];for (auto a : *collided) {if (a->equivalent(key)) {collided->remove(a);break;}}Cell<K, V> *cell = new Cell<K, V>(key, value);collided->push_back(cell);}V get(K key) {V v;int x = hashCodeOfKey(key);if (_items[x] == nullptr) {return v;}list<Cell<K, V>*> *collided = _items[x];for (auto a : *collided) {if (a->equivalent(key)) {return a->getValue();}}return v;}private:const int _MAX_SIZE = 10;vector<list<Cell<K, V>*>*> _items;
};

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544193.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring Boot 中实现跨域的 5 种方式,你一定要知道!

一、为什么会出现跨域问题出于浏览器的同源策略限制。同源策略&#xff08;Sameoriginpolicy&#xff09;是一种约定&#xff0c;它是浏览器最核心也最基本的安全功能&#xff0c;如果缺少了同源策略&#xff0c;则浏览器的正常功能可能都会受到影响。可以说Web是构建在同源策略…

stl:queue 源码_C ++ STL中的queue :: empty()和queue :: size()

stl:queue 源码In C STL, Queue is a type of container that follows FIFO (First-in-First-out) elements arrangement i.e. the elements which inserts first will be removed first. In queue, elements are inserted at one end known as "back" and are delet…

术中导航_密码术中的计数器(CTR)模式

术中导航The Counter Mode or CTR is a simple counter based block cipher implementation in cryptography. Each or every time a counter initiated value is encrypted and given as input to XOR with plaintext or original text which results in ciphertext block. Th…

Android社交类APP动态详情代码实现通用模板

&#xfeff;&#xfeff;Android社交类APP动态详情代码实现通用模板 Android平台上一些比较流行的社交类APP比如微信、陌陌等&#xff0c;都有动态详情页&#xff0c;在该页面&#xff0c;用户发表的动态详情&#xff0c;好友可以发起评论、点赞等等。这种设计在微信和陌陌上大…

聊聊并发编程的12种业务场景

前言并发编程是一项非常重要的技术&#xff0c;无论在面试&#xff0c;还是工作中出现的频率非常高。并发编程说白了就是多线程编程&#xff0c;但多线程一定比单线程效率更高&#xff1f;答&#xff1a;不一定&#xff0c;要看具体业务场景。毕竟如果使用了多线程&#xff0c;…

软件工程编码阶段_软件工程的编码阶段

软件工程编码阶段The coding phase in the software engineering paradigm is usually defined after the designing phase. In this phase, the developers or the coders have to implement the software design practically using any computer language(s) so that the sof…

梳理50道经典计算机网络面试题

我梳理了50道计算机网络面试题&#xff0c;每一道题目都特别经典&#xff0c;大厂也非常喜欢问。相信大家看完&#xff0c;会有新的收获滴~1. 说说HTTP常用的状态码及其含义&#xff1f;思路: 这道面试题主要考察候选人&#xff0c;是否掌握HTTP状态码这个基础知识点。不管是不…

A successful Git branching model

原文&#xff1a;http://nvie.com/posts/a-successful-git-branching-model/ In this post I present the development model that I’ve introduced for all of my projects (both at work and private) about a year ago, and which has turned out to be very successful. I…

一文详解读写锁

作者 | 磊哥来源 | Java面试真题解析&#xff08;ID&#xff1a;aimianshi666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;读写锁&#xff08;Readers-Writer Lock&#xff09;顾名思义是一把锁分为两部分&#xff1a;读锁和写锁&#xff0c…

ruby array_Ruby中带有示例的Array.keep_if方法

ruby arrayRuby Array.keep_if方法 (Ruby Array.keep_if Method) In the last articles, we have studied the Array methods namely Array.select, Array.reject and Array.drop_while, all these methods are non–destructive methods which means that they do not impose …

[实战]MVC5+EF6+MySql企业网盘实战(2)——用户注册

写在前面 上篇文章简单介绍了项目的结构&#xff0c;这篇文章将实现用户的注册。当然关于漂亮的ui&#xff0c;这在追后再去添加了&#xff0c;先将功能实现。也许代码中有不合适的地方&#xff0c;也只有在之后慢慢去优化了。 系列文章 [EF]vs15ef6mysql code first方式 [实战…

Calico IP_AUTODETECTION_METHOD

在 Calico 中&#xff0c;IP_AUTODETECTION_METHOD 的配置项用于指定 Calico 如何检测容器的 IP 地址。 一、kubernetes-internal-ip模式 其中&#xff0c;kubernetes-internal-ip 是一种特殊的模式&#xff0c;用于在 Kubernetes 环境中检测容器的 IP 地址。具体作用如下&…

下个十年高性能 JSON 库来了:fastjson2!

作者 | 磊哥来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;fastjson2 是 fastjson 项目的重要升级&#xff0c;目标是为下一个十年提供一个高性能的 JSON 库&#xff0c;同一套 API 支…

ascii非打印控制字符表_C程序打印ASCII表/图表

ascii非打印控制字符表什么是ASCII码&#xff1f; (What are ASCII Codes?) ASCII stands for American Standard Code for Information Interchange; it is a character encoding standards for information interchange in electronics communication. Each alphabets, spec…

THEOS的第一个TWeak的成功创建

THEOS的第一个TWeak的成功创建THEOS的第一个TWeak的成功创建参考资料:成功的创建一个TWeak的弹出步骤1:安装Xcode和Xcode command line步骤2:安装theosa:下载theos前,设置保存的路径:环境变量b:下载theosc:下载头文件d:下载ldid签名工具e:配置MoblieSubstrate环境f:安装dpkg步骤…

查询中,有没有可能多个索引一起用呢?

其实我们之前所讲的回表&#xff0c;就是两个索引树同时使用&#xff0c;先在二级索引树中搜索到对应的主键值&#xff0c;然后在再去主键索引树中查询完整的记录。但是我今天的问题是&#xff0c;两个不同的二级索引树&#xff0c;会同时生效吗&#xff1f;理论上来说&#xf…

ruby array_Ruby中带有示例的Array.sample()方法

ruby arrayArray.sample()方法 (Array.sample() Method) In this article, we will study about Array.sample() method. You all must be thinking the method must be doing something which is quite different from all those methods which we have studied. It is not as…

ThreadLocal夺命11连问

前言前一段时间&#xff0c;有同事使用ThreadLocal踩坑了&#xff0c;正好引起了我的兴趣。所以近期&#xff0c;我抽空把ThreadLocal的源码再研究了一下&#xff0c;越看越有意思&#xff0c;发现里面的东西还真不少。我把精华浓缩了一下&#xff0c;汇集成了下面11个问题&…

关于静态库、动态库的区别汇总

real framework中不可以使用类别 或 不可以不包含类文件real framework 中直接调用NSClassFromString函数会返回null 需要强制加载指定类 或 直接通过类名引用linux中静态库和动态库的区别一、不同库从本质上来说是一种可执行代码的二进制格式&#xff0c;可以被载入内存中执行…

PHP array_pad()函数与示例

PHP array_pad()函数 (PHP array_pad() function) array_pad() function is used to pad an array to given size with a specified value and returns a new array with a specified value. array_pad()函数用于将数组填充到具有指定值的给定大小&#xff0c;并返回具有指定值…