问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4116 访问。

不使用任何内建的哈希表库设计一个哈希映射

具体地说,你的设计应该包含以下的功能

put(key, value):向哈希映射中插入(键,值)的数值对。如果键对应的值已经存在,更新这个值。

get(key):返回给定的键所对应的值,如果映射中不包含这个键,返回-1。

remove(key):如果映射中存在这个键,删除这个数值对。

MyHashMap hashMap = new MyHashMap();

hashMap.put(1, 1);

hashMap.put(2, 2);

hashMap.get(1);            // 返回 1

hashMap.get(3);            // 返回 -1 (未找到)

hashMap.put(2, 1);         // 更新已有的值

hashMap.get(2);            // 返回 1

hashMap.remove(2);         // 删除键为2的数据

hashMap.get(2);            // 返回 -1 (未找到)

注意:

所有的值都在 [1, 1000000]的范围内。

操作的总数目在[1, 10000]范围内。

不要使用内建的哈希库。


Design a HashMap without using any built-in hash table libraries.

To be specific, your design should include these two functions:

put(key, value) : Insert a (key, value) pair into the HashMap. If the value already exists in the HashMap, update the value.

get(key): Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.

remove(key) : Remove the mapping for the value key if this map contains the mapping for the key.

MyHashMap hashMap = new MyHashMap();

hashMap.put(1, 1);

hashMap.put(2, 2);

hashMap.get(1);            // returns 1

hashMap.get(3);            // returns -1 (not found)

hashMap.put(2, 1);          // update the existing value

hashMap.get(2);            // returns 1

hashMap.remove(2);          // remove the mapping for 2

hashMap.get(2);            // returns -1 (not found)

Note:

All values will be in the range of [1, 1000000].

The number of operations will be in the range of [1, 10000].

Please do not use the built-in HashMap library.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4116 访问。

public class Program {

    public static void Main(string[] args) {
var hashMap = new MyHashMap(); hashMap.Put(1, 1);
hashMap.Put(2, 2);
hashMap.Get(1);
hashMap.Get(3);
hashMap.Put(2, 1);
hashMap.Get(2);
hashMap.Remove(2);
hashMap.Get(2); Console.ReadKey();
} public class MyHashMap { private int[] buckets; public MyHashMap() {
buckets = new int[1000000];
for(var i = 0; i < buckets.Length; i++) {
buckets[i] = -1;
}
} private int GetHashCode(int key) {
//用本身值作为散列值
return key;
} public void Put(int key, int value) {
buckets[GetHashCode(key)] = value;
} public int Get(int key) {
var res = buckets[GetHashCode(key)];
Console.WriteLine(res);
return res;
} public void Remove(int key) {
buckets[GetHashCode(key)] = -1;
} } }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4116 访问。

1
-1
1
-1

分析:

注意该题的测试用例中包含值为0的项目,并不是题目中[1, 1000000]的范围内。所以很无奈的在构造函数中初始化所有值为-1。

显而易见,MyHashMap解法中的Put、Get和Remove三个方法均为  的常量时间复杂度。

C#LeetCode刷题之#706-设计哈希映射(Design HashMap)的更多相关文章

  1. LeetCode 706:设计哈希映射 Design HashMap

    题目: 不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get ...

  2. [Swift]LeetCode706. 设计哈希映射 | Design HashMap

    Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...

  3. Java实现 LeetCode 706 设计哈希映射(数组+链表)

    706. 设计哈希映射 不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新 ...

  4. C#LeetCode刷题之#705-设计哈希集合​​​​​​​(Design HashSet)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4114 访问. 不使用任何内建的哈希表库设计一个哈希集合 具体地说 ...

  5. C#LeetCode刷题之#622-设计循环队列​​​​​​​(Design Circular Queue)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4126 访问. 设计你的循环队列实现. 循环队列是一种线性数据结构 ...

  6. C#LeetCode刷题-设计

    设计篇 # 题名 刷题 通过率 难度 146 LRU缓存机制   33.1% 困难 155 最小栈 C#LeetCode刷题之#155-最小栈(Min Stack) 44.9% 简单 173 二叉搜索 ...

  7. C#LeetCode刷题-哈希表

    哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串   24.2% 中等 18 四数之和   ...

  8. C#LeetCode刷题-队列

    队列篇 # 题名 刷题 通过率 难度 363 矩形区域不超过 K 的最大数值和   27.2% 困难 621 任务调度器   40.9% 中等 622 设计循环队列 C#LeetCode刷题之#622 ...

  9. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

随机推荐

  1. Java应用服务器之tomcat基础配置(一)

    前文我们聊到了java相关重要组件和它们之间的关系以及jdk.tomcat部署回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13302938.html:今天我们 ...

  2. 什么是A站、B站、C站、D站、E站、F站、G站、HIJKLM站N站?

    A站AcFun弹幕视频网,简称“A站”,成立于2007年6月,取意于Anime Comic Fun,是中国大陆第一家弹幕视频网站.A站以视频为载体,逐步发展出基于原生内容二次创作的完整生态,拥有高质量 ...

  3. MSSQL系列 (二):表相关操作、列操作、(唯一、主键、默认、检查、外键、非空)约束、临时表

    1.创建表 --创建学生班级表 create table StuClass ( ClassId int primary key, --班级ID 主键约束 ClassName nvarchar(30) ...

  4. JQuery如何在验证表单失败的情况下阻止表单提交

    自定义验证时,使用了return false和event.preventDefault(),但是验证失败之后表单还是提交了 这个问题我也碰到了,尝试了多次也没有用,在调试的时候也发现确实return了 ...

  5. vue传参方式

    //query传参,使用name跳转   this.$router.push({       name:'second',       query: {         queryId:'201808 ...

  6. ISE和modelsim的配合

    modelsim好强呀,我在ISE中在编写时clk不小心把input写成了inout,ISE也没有给我报错:在modelsim中仿真时提示出这个错误了!

  7. 解决element上传功能清除单个文件的问题

    今天,在使用 element 实现一个上传文件的功能. 接下来,要对上传的文件列表,实现删除单文件的功能. 看了 element 开发文档,发现 on-remove 没有特别的详细的说明,刚开始使用 ...

  8. 艺术鬼才,Unicode 字符还能这么玩?

    上周的时候,朋友圈的直升飞机不知道为什么就火了,很多朋友开着各种花式飞机带着起飞. 还没来得及了解咋回事来着,这个直升飞机就到的微博热搜. 后面越来越多人开来他们的直升飞机,盘旋在朋友圈上方.于是很多 ...

  9. ngx lua获取时间戳的几种方式

    原创自由de单车 最后发布于2017-02-14 14:58:43 阅读数 18218 收藏 在ngx_lua里,获取时间相关信息的方式大概有4种(见下面代码): print(string.forma ...

  10. link小图标以及表格的用法基础

    一.网页小图标的实现 实例: 实现方式: 效果: 二.表格基础 1.表格的组合标签 常用: table tr td caption ①table属性 border  边框 width  宽度 默认按照 ...