[LeetCode&Python] Problem 706. Design HashMap
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.
Example:
MyHashMap hashMap = new MyHashMap();
hashMap.put(1, 1);
hashMap.put(2, 2);
hashMap.get(1); // returns 1
hashMap.get(3); // returns -1 (not found)
hashMap.put(2, 1); // update the existing value
hashMap.get(2); // returns 1
hashMap.remove(2); // remove the mapping for 2
hashMap.get(2); // returns -1 (not found)
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.
class MyHashMap(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self._data={}
def put(self, key, value):
"""
value will always be non-negative.
:type key: int
:type value: int
:rtype: void
"""
self._data[key]=value
def get(self, key):
"""
Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
:type key: int
:rtype: int
"""
if key in self._data:
return self._data[key]
else:
return -1
def remove(self, key):
"""
Removes the mapping of the specified value key if this map contains a mapping for the key
:type key: int
:rtype: void
"""
if key in self._data:
del self._data[key]
# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key,value)
# param_2 = obj.get(key)
# obj.remove(key)
[LeetCode&Python] Problem 706. Design HashMap的更多相关文章
- [LeetCode&Python] Problem 705. Design HashSet
Design a HashSet without using any built-in hash table libraries. To be specific, your design should ...
- 【Leetcode_easy】706. Design HashMap
problem 706. Design HashMap solution1: class MyHashMap { public: /** Initialize your data structure ...
- 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] 706. Design HashMap
题目 Design a HashMap without using any built-in hash table libraries. Implement the MyHashMap class: ...
- 【LeetCode】706. Design HashMap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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&Python] Problem 703. Kth Largest Element in a Stream
Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...
随机推荐
- -L、-rpath和-rpath-link的区别
链接器ld的选项有 -L,-rpath 和 -rpath-link,看了下 man ld,大致是这个意思: -L:: “链接”的时候去找的目录,也就是所有的 -lFOO 选项里的库,都会先从 -L ...
- POJ 2243 Knight Moves(BFS)
POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...
- 【Jenkins】Jenkins安装修改默认路径和端口的方法
一.修改默认的jenkins安装路径 因为jenkins默认安装在c盘 C:\Users\Administrator\.jenkins下,那怎样将安装路径修改至d盘呢? 新建一个系统变量:JENKIN ...
- 一些做vue前端的经验
1.先赋值,后渲染 场景:表格渲染中,一般都是这样把json的东西传给table的 this.tableData = json.data.rows 然后的话我们一般会在渲染前对json中的数据做一些转 ...
- day037 行记录的操作
1.库操作 2.表操作 3.行操作 1.库操作 1)创建数据库 语法: create database 数据库名 charset utf8; 数据库命名规则: 由数字,字母,下划线,@,#,$ 等组成 ...
- 一个表中多个字段对应另一个表的ID(SQL查询)
A数据表中多个字段对应B数据表的ID, 现在要把B表的其他字段一起查询出来 一.数据表: 1.SPEED_DETECTION_ROAD 它的 START_POINT_ID 和 END_POINT_ID ...
- vue.set动态新增对象属性,触发dom渲染
当我们给一个props或者data中被观测的对象添加一个新的属性的时候,不能直接添加,必须使用Vue.set方法 /** * ==== 选择产品 ==== * 因为vue实现双向数据绑定的机制是数据劫 ...
- java泛型讲解
原文: https://blog.csdn.net/briblue/article/details/76736356 泛型,一个孤独的守门者. 大家可能会有疑问,我为什么叫做泛型是一个守门者.这其实是 ...
- 7.7 C++基本关联式容器
参考:http://www.weixueyuan.net/view/6404.html 总结: 基本的关联式容器主要有:set.multiset.map和multimap,这四种容器可以分为两组:ma ...
- 5.2 C++重载操作符的优先级
参考:http://www.weixueyuan.net/view/6380.html 总结: 重载操作符不能改变操作符的优先级和语法特性. 重载操作符不能改变操作符的优先级和语法特性.例如上一节复数 ...