[leetcode] 706. Design HashMap
题目
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 thekey
already exists in the map, update the correspondingvalue
.int get(int key)
returns thevalue
to which the specifiedkey
is mapped, or-1
if this map contains no mapping for thekey
.void remove(key)
removes thekey
and its correspondingvalue
if the map contains the mapping for thekey
.
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 toput
,get
, andremove
.
思路
关键在于采用高效的散列函数。
代码
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的更多相关文章
- LeetCode 706 Design HashMap 解题报告
题目要求 Design a HashMap without using any built-in hash table libraries. To be specific, your design s ...
- LeetCode 706. Design HashMap (设计哈希映射)
题目标签:HashMap 题目让我们设计一个 hashmap, 有put, get, remove 功能. 建立一个 int array, index 是key, 值是 value,具体看code. ...
- Leetcode PHP题解--D75 706. Design HashMap
2019独角兽企业重金招聘Python工程师标准>>> D75 706. Design HashMap 题目链接 706. Design HashMap 题目分析 自行设计一个has ...
- 706. Design HashMap - LeetCode
Question 706. Design HashMap Solution 题目大意:构造一个hashmap 思路:讨个巧,只要求key是int,哈希函数选择f(x)=x,规定key最大为100000 ...
- 【Leetcode_easy】706. Design HashMap
problem 706. Design HashMap solution1: class MyHashMap { public: /** Initialize your data structure ...
- 【LeetCode】706. Design HashMap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode&Python] Problem 706. Design HashMap
Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...
- 706. Design HashMap 实现哈希表
[抄题]: public MyHashMap() { 主函数里面是装非final变量的,如果没有,可以一个字都不写 } [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: ...
- LeetCode:用HashMap解决问题
LeetCode:用HashMap解决问题 Find Anagram Mappings class Solution { public int[] anagramMappings(int[] A, i ...
随机推荐
- 【Java】idea同时运行多个一样的类
点击"Edit Configurations..." 在左侧选中需要重复运行的类 单击"Modify options" 选择"Allow multip ...
- 禁止mysql自动更新
每到00:00时,MySQL弹出小黑框 这是mysql在自动检测更新 右键"此电脑",点击"管理" 依此操作即可
- Jetpack架构组件学习(4)——APP Startup库的使用
最近在研究APP的启动优化,也是发现了Jetpack中的App Startup库,可以进行SDK的初始化操作,于是便是学习了,特此记录 原文:Jetpack架构组件学习(4)--App Startup ...
- HPC+时代,携手亚马逊云科技,共赴数字化升级的星辰大海!
高性能计算(HPC)和云计算曾是两个"平行世界",各自演绎着精彩,却鲜有交集. 传统上,HPC主要应用于大规模计算,如天气预报.石油勘探.药物研发等.这些任务通常借助超级计算机或计 ...
- Html飞机大战(六):移动飞机
好家伙,这篇移动主角 我们先来看看一个好东西, addEventListener() 方法 (他真的很好用) 我们直译一下,就叫他添加事件监听器方法 而可监听的对象就有很多啦 我们来了解一 ...
- Base64加密、解密
#region Base64加密方法 /// <summary> /// Base64加密,采用utf8编码方式加密 /// </summary> /// <param ...
- 《Java基础——数组的定义与使用》
Java基础--数组的定义与使用 一. 一维数组: 格式一: 数组类型 数组变量[]=new 数据类型[长度]; //需要后续赋值,且后续赋值时只能为单个元素赋值. 或 数组类型 数组变量 ...
- Beats:最佳实践
转载自: https://blog.csdn.net/UbuntuTouch/article/details/105933699
- 阿里云SLB的健康检查配置
若阿里云SLB健康检查异常,则默认SLB无法访问到后端ECS,也就意味着通过SLB访问后端ECS上部署的服务会报502 gateway. 另一种办法是关闭健康检查(不推荐) 简要说明: SLB通过配置 ...
- filebeat中增加其他参数用来区分不同的日志来源示例
filebeat.yml配置文件参数 在source里加入了一个fields叫做"appach: true". filebeat.inputs: - type: log enabl ...