题目:

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

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

  • put(key, value):向哈希映射中插入(键,值)的数值对。如果键对应的值已经存在,更新这个值。
  • get(key):返回给定的键所对应的值,如果映射中不包含这个键,返回-1。
  • remove(key):如果映射中存在这个键,删除这个数值对。

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

To be specific, your design should include these 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); // 返回 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]范围内。
  • 不要使用内建的哈希库。

Note:

  • All keys and values will be in the range of [0, 1000000].
  • The number of operations will be in the range of [1, 10000].
  • Please do not use the built-in HashMap library.

解题思路:

​ 与设计哈希集合一题相似,只需要将布尔类型数组改成 int 整型数组,元素索引位置为Key值,元素值为Value值。

​ 题目中要求Key不存在时返回 -1 ,Python中可以直接初始化值为 -1 的长度为 1000001 的数组,直接返回 Value值即可。其他语言初始化数组后元素值默认为0,可以遍历一遍把值改为 -1,或存储值为真实值加 1,返回 Value - 1,如果 Key 不存在时 Value 为 0,返回 Value - 1 = -1,符合要求。

代码:

Java:

class MyHashMap {
private int[] hashMap;
/** Initialize your data structure here. */
public MyHashMap() {
this.hashMap=new int[1000001];
} /** value will always be non-negative. */
public void put(int key, int value) {
hashMap[key] = value+1;//存储真实值加 1
} /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
public int get(int key) {
return hashMap[key] - 1;//返回存储值为 -1 得到真实值
} /** Removes the mapping of the specified value key if this map contains a mapping for the key */
public void remove(int key) {
hashMap[key] = 0;
}
}

Python:

class MyHashMap:

    def __init__(self):
"""
Initialize your data structure here.
"""
self.hash_table = [-1]*1000001 def put(self, key: int, value: int) -> None:
"""
value will always be non-negative.
"""
self.hash_table[key] = value def get(self, key: int) -> int:
"""
Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
"""
return self.hash_table[key]#直接返回Value def remove(self, key: int) -> None:
"""
Removes the mapping of the specified value key if this map contains a mapping for the key
"""
self.hash_table[key] = -1

欢迎关注微.信公.众号:爱写Bug

LeetCode 706:设计哈希映射 Design HashMap的更多相关文章

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

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

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

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

  3. 领扣(LeetCode)设计哈希映射 个人题解

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

  4. Leetcode706.Design HashMap设计哈希映射

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

  5. Java实现 LeetCode 705 设计哈希集合(使用数组保存有没有被用过)

    705. 设计哈希集合 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中 ...

  6. LeetCode 706. Design HashMap (设计哈希映射)

    题目标签:HashMap 题目让我们设计一个 hashmap, 有put, get, remove 功能. 建立一个 int array, index 是key, 值是 value,具体看code. ...

  7. LeetCode 705:设计哈希集合 Design HashSet

    题目: 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中是否存在这个值. ...

  8. [Swift]LeetCode705. 设计哈希集合 | Design HashSet

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

  9. C#LeetCode刷题-哈希表

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

随机推荐

  1. vim简单操作命令

    vim简单操作命令: 开启编辑:按“i”或者“Insert”键 退出编辑:“Esc”键 退出vim:“:q” 保存vim:“:w” 保存退出vim:“:wq” 不保存退出vim:“:q!” 查看当前系 ...

  2. Redis集群模式下的redis-py-cluster方式读写测试

    与MySQL主从复制,从节点可以分担部分读压力不一样,甚至可以增加slave或者slave的slave来分担读压力,Redis集群中的从节点,默认是不分担读请求的,从节点只作为主节点的备份,仅负责故障 ...

  3. git上传本地项目到github或者gitlib(两个是一样的)。

    第一步:在github上面创建一个repository   点击create就好了.然后会出现下面的页面. 第三步:打开你所在文件夹,或者是新建的文件夹(用来做仓库的)右键会出现下面  选用git B ...

  4. MySQL数据库:聚合函数的使用

    聚合函数 max() 最大值 min() 最小值 avg() 平均值 sum() 求和 count() 符合条件数据的数目 聚合函数不能嵌套使用 # 在统计时字段内没有满足条件的数值只有count返回 ...

  5. 初级模拟电路:4-1 BJT交流分析概述

    回到目录 BJT晶体管的交流分析(也叫小信号分析)是模拟电路中的一个难点,也可以说是模电中的一个分水岭.如果你能够把BJT交流分析的原理全都搞懂,那之后的学习就是一马平川了.后面的大部分内容,诸如:场 ...

  6. [洛谷P1279][题解]字串距离

    题目戳我 很明显的这题是一道dp,主要讲一下几个细节 1.初始化 我们需要初始化边界情况也就是一个字符串为空的情况 #----------# #----------# A:aaaaaa A:□□□□□ ...

  7. ubuntu vscode 写一个C++程序

    博客转载:https://blog.csdn.net/weixin_43374723/article/details/84064644 Visual studio code是微软发布的一个运行于 Ma ...

  8. 【Hash一致性算法】什么是Hash一致性算法

    目录 1. 一致性Hash算法简介 环形Hash空间 把数据通过一定的hash算法处理后映射到环上 将机器通过hash算法映射到环上 机器的删除与添加 平衡性 本文转载自博客 1. 一致性Hash算法 ...

  9. Python迭代器(函数名的应用,新版格式化输出)

    1. 函数名的运用 ​ 你们说一下,按照你们的理解,函数名是什么? ​ 函数名的定义和变量的定义几乎一致,在变量的角度,函数名其实就是一个变量,具有变量的功能:可以赋值:但是作为函数名他也有特殊的功能 ...

  10. iOS 自定义导航栏

    参考链接:https://blog.csdn.net/huanglinxiao/article/details/100537988 demo下载地址:https://github.com/huangx ...