C#LeetCode刷题之#706-设计哈希映射(Design HashMap)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- LeetCode 706:设计哈希映射 Design HashMap
题目: 不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get ...
- [Swift]LeetCode706. 设计哈希映射 | Design HashMap
Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...
- Java实现 LeetCode 706 设计哈希映射(数组+链表)
706. 设计哈希映射 不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新 ...
- C#LeetCode刷题之#705-设计哈希集合(Design HashSet)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4114 访问. 不使用任何内建的哈希表库设计一个哈希集合 具体地说 ...
- C#LeetCode刷题之#622-设计循环队列(Design Circular Queue)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4126 访问. 设计你的循环队列实现. 循环队列是一种线性数据结构 ...
- C#LeetCode刷题-设计
设计篇 # 题名 刷题 通过率 难度 146 LRU缓存机制 33.1% 困难 155 最小栈 C#LeetCode刷题之#155-最小栈(Min Stack) 44.9% 简单 173 二叉搜索 ...
- C#LeetCode刷题-哈希表
哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串 24.2% 中等 18 四数之和 ...
- C#LeetCode刷题-队列
队列篇 # 题名 刷题 通过率 难度 363 矩形区域不超过 K 的最大数值和 27.2% 困难 621 任务调度器 40.9% 中等 622 设计循环队列 C#LeetCode刷题之#622 ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
随机推荐
- 通过hmail搭建一个内网测试的邮件服务器
我们测试的软件基本上都是支持邮件功能,如果你的测试环境是在外网的话那还好说,可以直接使用QQ邮箱.163邮箱等.但是如果是测试环境在内网,无法直接访问到外网的时候,搭建一个邮件服务器就很有必 ...
- easyUI时间控件
##=============================JSP======================================<div class="labelw l ...
- Linux下diff工具
目录 CentOS 7为例 Meld DiffMerge KDiff3 Kompare CentOS 7为例 Meld Installation 官网 $ sudo yum install meld; ...
- java 之 实例方法和类方法
类方法:使用static修饰(静态方法),属于整个类的,不是属于某个实例的,只能处理static域或调用static方法: 实例方法:属于对象的方法,由对象来调用. 判断类方法,类方法的前面有stat ...
- Ethical Hacking - POST EXPLOITATION(4)
PIVOTING Use the hacked device as a pivot. Try to gain access to other devices in the network. Tool: ...
- tensorflow对鸢尾花进行分类——人工智能入门篇
tensorflow之对鸢尾花进行分类 任务目标 对鸢尾花数据集分析 建立鸢尾花的模型 利用模型预测鸢尾花的类别 环境搭建 pycharm编辑器搭建python3.* 第三方库 tensorflow1 ...
- 在ShareX里添加流浪图床
这里以咱流浪图床为例哈:-D 上传目标类型:图像.文件 请求方法:POST 请求URL:https://p.sda1.dev/api/v1/upload_external_noform URL参数:名 ...
- Centos 7 静态IP设置
1.编辑 ifcfg-eth0 文件,vim 最小化安装时没有被安装,需要自行安装不描述. # vim /etc/sysconfig/network-scripts/ifcfg-eth0 2.修改如下 ...
- 启动扫描闪退,因为忘了在manifest里申请手机镜头使用许可了。
启动扫描闪退,因为忘了在manifest里申请手机镜头使用许可了.
- .NET Core 微服务—API网关(Ocelot) 教程 [二]
上篇文章(.NET Core 微服务—API网关(Ocelot) 教程 [一])介绍了Ocelot 的相关介绍. 接下来就一起来看如何使用,让它运行起来. 环境准备 为了验证Ocelot 网关效果,我 ...