题目:

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

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

  • 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. 基于Redis扩展模块的布隆过滤器使用

    什么是布隆过滤器?它实际上是一个很长的二进制向量和一系列随机映射函数.把一个目标元素通过多个hash函数的计算,将多个随机计算出的结果映射到不同的二进制向量的位中,以此来间接标记一个元素是否存在于一个 ...

  2. packstack-ironic

    安装openstack Pike版本, 其它版本安装方法类似. centos7.6 packstack目前对NetworkManager 还不支持,我们修改下配置: systemctl disable ...

  3. Unity3D 截取6面图 做全景图脚本

    using System.Collections;using System.Collections.Generic;using UnityEditor;using UnityEngine; publi ...

  4. WASM 成为 HTML、CSS 与 JS 之后的第 4 门 Web 语言

    大家都知道,万维网联盟 W3C 认证的 Web 语言有 HTML.CSS 与 JavaScript,而近日联盟正式宣布 WebAssembly 核心规范(WebAssembly Core Specif ...

  5. (绿色)修正版gooflow流程解决方案(源码分享+在线演示+UI地址下载)

    gooflow出现挖矿机木马,请勿随意去其他网站下载!!! 一.功能简介 gooflow功能清单1.自定义流程绘制2.自定义属性添加3.支持3种步骤类型普通审批步骤自动决策步骤手动决策步骤 4.决策方 ...

  6. 火车票买不到?看我用python监控票源

    同事说最近火车票难买,我就帮他用个脚本监控 一下. 最近高铁票比较难买,还有什么候补.要不停的刷,才有时候可以捡漏.有时候明明候补了,到快开车告诉你余票不足,候补失败. 凡事预则立,我打算写个脚本提前 ...

  7. sublime插件开发教程4

    写几个简单的例子详解下 import sublime import sublime_plugin class ExampleCommand(sublime_plugin.TextCommand): d ...

  8. 动态数组原理【Java实现】(六)

    前言 接下来我们进入集合学习,看过很多文章一上来就是讲解原理感觉会特别枯燥,任何成熟解决方案的出现都是为了解决问题,若通过实际问题引入然后再来讲解原理想必学起来必定事半功倍,从我写博客的那一天起,我就 ...

  9. ASP.NET MVC教程三:ASP.NET MVC部署方式

    ASP.NET MVC编写的程序需要部署到IIS上面才能进行访问,部署方式分为两种. 一.直接用源代码部署 第一种方式可以直接使用源代码进行部署.部署步骤: 1.新建网站 在IIS里面选择网站,然后右 ...

  10. ASP.NET Core Web 应用程序系列(四)- ASP.NET Core 异步编程之async await

    PS:异步编程的本质就是新开任务线程来处理. 约定:异步的方法名均以Async结尾. 实际上呢,异步编程就是通过Task.Run()来实现的. 了解线程的人都知道,新开一个线程来处理事务这个很常见,但 ...