LeetCode 706:设计哈希映射 Design HashMap
题目:
不使用任何内建的哈希表库设计一个哈希映射
具体地说,你的设计应该包含以下的功能
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的更多相关文章
- Java实现 LeetCode 706 设计哈希映射(数组+链表)
706. 设计哈希映射 不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新 ...
- [Swift]LeetCode706. 设计哈希映射 | Design HashMap
Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...
- 领扣(LeetCode)设计哈希映射 个人题解
不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get(key ...
- Leetcode706.Design HashMap设计哈希映射
不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get(key ...
- Java实现 LeetCode 705 设计哈希集合(使用数组保存有没有被用过)
705. 设计哈希集合 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中 ...
- LeetCode 706. Design HashMap (设计哈希映射)
题目标签:HashMap 题目让我们设计一个 hashmap, 有put, get, remove 功能. 建立一个 int array, index 是key, 值是 value,具体看code. ...
- LeetCode 705:设计哈希集合 Design HashSet
题目: 不使用任何内建的哈希表库设计一个哈希集合 具体地说,你的设计应该包含以下的功能 add(value):向哈希集合中插入一个值. contains(value) :返回哈希集合中是否存在这个值. ...
- [Swift]LeetCode705. 设计哈希集合 | Design HashSet
Design a HashSet without using any built-in hash table libraries. To be specific, your design should ...
- C#LeetCode刷题-哈希表
哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串 24.2% 中等 18 四数之和 ...
随机推荐
- Python学习笔记六(免费获取代理IP)
为获取网上免费代理IP,闲的无聊,整合了一下,免费从三个代理网站获取免费代理IP,目的是在某一代理网站被限制时,仍可从可以访问的其他网站上获取代理IP.亲测可用哦!^_^ 仅供大家参考,以下脚本可添 ...
- python操作excel表
1.新增表并添加数据: 2.给工作表添加表名称,给表数据添加格式: import xlsxwriterdatas=(['Rent',1000], ['Gas',100], ['fish','画画'], ...
- 5G技术发展迅猛,亚博电竞(yabo055)搭上科技快车
要说当前互联网科技最为令人期待的当属yabo055点康母的5G技术了.自2018年5G标准确定以来,民众就对5G非常的期待,而亚博电竞早已意识到了5G时代的来临势不可挡,早已着手将5G运用于网站和游戏 ...
- LeetCode 第70题动态规划算法
导言 看了 动态规划(https://www.cnblogs.com/fivestudy/p/11855853.html)的帖子,觉得写的很好,记录下来. 动态规划问题一直是算法面试当中的重点和难点, ...
- ROS基础-基本概念和简单工具(1)
1.什么是ROS? Robot operating System ,简单说机器人操作系统,弱耦合的分布式进程框架,通过进程间的消息传递和管理.实现硬件抽象和设备控制. 2.节点(node) node ...
- 【HTML】---常用标签(1)
Html常用标签(1) 重新整理学习下前端知识从Html标签开始.我们先看HTML 骨架格式: <!DOCTYPE html> <!--这句话就是告诉我们使用哪个html版本--&g ...
- Web前端基础(15):jQuery基础(二)
1. jQuery选择器 jQuery选择器是jQuery强大的体现,它提供了一组方法,让我们更加方便的获取到页面中的元素. 1.1 基本选择器 例子如下: <!DOCTYPE html> ...
- 在python操作数据库中游标的使用方法
cursor就是一个Cursor对象,这个cursor是一个实现了迭代器(def__iter__())和生成器(yield)的MySQLdb对象,这个时候cursor中还没有数据,只有等到fetcho ...
- 用XHR简单封装一个axios
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ES6-Set的增加、查找、删除、遍历、查看长度、数组去重
set 是es6新出的一种数据结构,里边放的是数组. 作用:去重(set里边的数组不能重复) MDN:Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用. 总结: 1.成员唯一.无序且 ...