题目

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

Implement the MyHashMap class:

  • MyHashMap() initializes the object with an empty map.
  • void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.
  • int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
  • void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.

Example 1:

Input
["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
Output
[null, null, null, 1, -1, null, 1, null, -1] Explanation
MyHashMap myHashMap = new MyHashMap();
myHashMap.put(1, 1); // The map is now [[1,1]]
myHashMap.put(2, 2); // The map is now [[1,1], [2,2]]
myHashMap.get(1); // return 1, The map is now [[1,1], [2,2]]
myHashMap.get(3); // return -1 (i.e., not found), The map is now [[1,1], [2,2]]
myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value)
myHashMap.get(2); // return 1, The map is now [[1,1], [2,1]]
myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]]
myHashMap.get(2); // return -1 (i.e., not found), The map is now [[1,1]]

Constraints:

  • 0 <= key, value <= 106
  • At most 104 calls will be made to put, get, and remove.

思路

关键在于采用高效的散列函数。

代码

python版本:

# Runtime: 9894 ms, faster than 5.00% of Python3 online submissions for Design HashMap.
class MyHashMap: def __init__(self):
self.hashTable = [[]]*10000 def put(self, key: int, value: int) -> None:
hash = key % 10000
for i in range(len(self.hashTable[hash])):
if self.hashTable[hash][i][0] == key:
self.hashTable[hash][i] = (key, value)
return
self.hashTable[hash].append((key, value)) def get(self, key: int) -> int:
hash = key % 10000
for i in range(len(self.hashTable[hash])):
if self.hashTable[hash][i][0] == key:
return self.hashTable[hash][i][1]
return -1 def remove(self, key: int) -> None:
hash = key % 10000
for i in range(len(self.hashTable[hash])):
if self.hashTable[hash][i][0] == key:
self.hashTable[hash].pop(i)
return

[leetcode] 706. Design HashMap的更多相关文章

  1. LeetCode 706 Design HashMap 解题报告

    题目要求 Design a HashMap without using any built-in hash table libraries. To be specific, your design s ...

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

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

  3. Leetcode PHP题解--D75 706. Design HashMap

    2019独角兽企业重金招聘Python工程师标准>>> D75 706. Design HashMap 题目链接 706. Design HashMap 题目分析 自行设计一个has ...

  4. 706. Design HashMap - LeetCode

    Question 706. Design HashMap Solution 题目大意:构造一个hashmap 思路:讨个巧,只要求key是int,哈希函数选择f(x)=x,规定key最大为100000 ...

  5. 【Leetcode_easy】706. Design HashMap

    problem 706. Design HashMap solution1: class MyHashMap { public: /** Initialize your data structure ...

  6. 【LeetCode】706. Design HashMap 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. [LeetCode&Python] Problem 706. Design HashMap

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

  8. 706. Design HashMap 实现哈希表

    [抄题]: public MyHashMap() {  主函数里面是装非final变量的,如果没有,可以一个字都不写 } [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: ...

  9. LeetCode:用HashMap解决问题

    LeetCode:用HashMap解决问题 Find Anagram Mappings class Solution { public int[] anagramMappings(int[] A, i ...

随机推荐

  1. 尝试理解Linux容器进程与宿主机共享内核到底是什么意思?

    背景 近期接触容器技术时,经常看到各类比较容器与虚拟机区别的文章中会提到:容器是共享宿主机的内核,而虚拟机则是拥有自己独立的内核,所以不可能在Linux上用容器运行windows,但是用虚拟机则可以. ...

  2. Java代码自动计算机

    答案(这里面包括main方法不要写重了),copy即可使用: public static void main(String[] args) { while (true){ System.out.pri ...

  3. 排序算法整理C++(初赛)

    排序算法整理 常见考点 将一个乱掉的字符串排回有序(以交换为基本操作)的最少操作,就是冒泡排序. 排序算法的稳定性 排序算法的时间复杂度 排序算法的稳定性 稳定性是指排序前两个元素a1 = a2,a1 ...

  4. Karmada v1.3:更优雅 更精准 更高效

    摘要:最新发布的1.3版本中,Karmada重新设计了应用跨集群故障迁移功能,实现了基于污点的故障驱逐机制,并提供平滑的故障迁移过程,可以有效保障服务迁移过程的连续性(不断服). 本文分享自华为云社区 ...

  5. 利用c++编写bp神经网络实现手写数字识别详解

    利用c++编写bp神经网络实现手写数字识别 写在前面 从大一入学开始,本菜菜就一直想学习一下神经网络算法,但由于时间和资源所限,一直未展开比较透彻的学习.大二下人工智能课的修习,给了我一个学习的契机. ...

  6. 《Java基础——制表符》

    Java基础--制表符       规则: 若前面输出内容不为8的倍数,则通过空格补全. 不足八位,补全八位.   例一:不足八位: System.out.println("123456&q ...

  7. Linux 使用 Systemd 管理进程服务

    转载自:https://mp.weixin.qq.com/s/e-_PUNolUm22-Uy_ZjpuEA systemd 介绍 systemd是目前Linux系统上主要的系统守护进程管理工具,由于i ...

  8. Traefik2.X 版本 中 URL Rewrite 的使用

    文章转载自:https://mp.weixin.qq.com/s?__biz=MzU4MjQ0MTU4Ng==&mid=2247484594&idx=1&sn=becbe567 ...

  9. windows系统下使用bat脚本文件设置MySQL系统环境变量

    说明:在一个bat文件中设置tomcat环境变量后,不能直接使用,需要另起一个bat文件才能使用 号开头的行不要写在bat文件中 # 这个bat文件实现的功能:设置环境变量 @echo off set ...

  10. js内置禁用按钮 disabled

    按钮在监听到disabled后面的布尔值就可以实现是否禁用 一: <button :disabled="book.count <= 1" @click="de ...