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的更多相关文章

  1. [LeetCode&Python] Problem 705. Design HashSet

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

  2. 【Leetcode_easy】706. Design HashMap

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

  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] 706. Design HashMap

    题目 Design a HashMap without using any built-in hash table libraries. Implement the MyHashMap class: ...

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

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

  7. LeetCode 706 Design HashMap 解题报告

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

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

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

  9. [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 ...

随机推荐

  1. [LeetCode] Network Delay Time 网络延迟时间——最短路算法 Bellman-Ford(DP) 和 dijkstra(本质上就是BFS的迭代变种)

    There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edges ti ...

  2. SIMD指令集——一条指令操作多个数,SSE,AVX都是,例如:乘累加,Shuffle等

    SIMD指令集 from:https://zhuanlan.zhihu.com/p/31271788 SIMD,即Single Instruction, Multiple Data,一条指令操作多个数 ...

  3. Qt Widgets——菜单和菜单栏

    主窗口MainWindow需要菜单栏QMenuBar及菜单QMenu来组成自身,一般应用程序的所有功能都能在菜单中找到.接下来就来说说它们. QMenu 它添加了很多动作QAction,并用自身组成了 ...

  4. nc(netcat)扫描开放端口

    探测单个端口是否开放可以用telnet,专业探测端口可以用Nmap,而对于非渗透用途的Linux可以直接用netcat. 1.使用netcat探测端口是否开放 nc -z -v - #z代表不交互要不 ...

  5. dynamic load jar and init spring

    public class SpringLoader { private Map<String, Class<?>> classMap = new HashMap<> ...

  6. Qt之获取子部件

    Qt5.10 QList<QObject*> list_children = this->children(); for(int i=0;i<list_children.siz ...

  7. linux nat style

      1● nat style 2● link style    

  8. C#调用接口返回json数据中含有双引号 或其他非法字符的解决办法

    这几天,调用别人接口返回json数据含有特殊符号(双引号),当转换成json对象总是报错, json字符格式如下 { "BOXINFO":[ { ", "ITE ...

  9. Jquery的jqzoom插件的使用(图片放大镜)

    今天学习一下,图片放大镜功能,需要使用插件JQzoom 引入文件 <script type="text/javascript" src="js/jquery.min ...

  10. 在引用的laravel的@include子模板中传递参数

    调用传参: @include("message",['msg'=>'中国']) 在message子模板中调用msg的值: {{msg}}