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. oracle 11g创建数据库教程

    cd /oracle/app/oracle/product//dbhome_1/bin ./dbca 自定义用户表空间大小. 安装过程半个小时是需要的. 2.配置oracle系统用户环境变量 使用vi ...

  2. Notes on Large-scale Video Classification with Convolutional Neural Networks

    Use bigger datasets for CNN in hope of better performance. A new data set for sports video classific ...

  3. VIM编辑配置文件基本操作

    vim  /etc/apt/sources.list 按insert键进入编辑状态 编辑完成以后按ESC退出编辑状态 输入 ":"进入命令状态,常用命令: 1.W:write ,写 ...

  4. CAD(镜像对象)(镜像后的微调)(门窗标注)5.14

    由于绘制的cad图形整体是对称的,所以先画了一半,然后再镜像过去.沿着某一对称轴. 注意:1.有些三维内容在二维是选择不上的.2.对称轴上的柱子和墙体之类的不能选中.把轴网和标注的图层锁起来.冻结起来 ...

  5. python之路 ---计算机硬件基础

    计算机(computer)俗称电脑,是现代一种用于高速计算的电子计算机器,可以进行数值计算,又可以进行逻辑计算,还具有存储记忆功能.是能够按照程序运行,自动.高速处理海量数据的现代化智能电子设备.一个 ...

  6. python 学习 面向对象编程

    面向对象编程---oop,是一种编程思想,oop把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数的顺序执行为了简化程序设计 ...

  7. shell脚本中出现图形化界面

    http://www.ttlsa.com/shell/how-to-create-dialog-boxes-in-interactive-shell-script/

  8. SpringMVC中文乱码的解决办法

    中文乱码分类: (1)按照请求分类: GET请求乱码 POST请求乱码 (2)按照乱码位置分类 从前台传到后台的数据乱码(存储到数据库中的数据乱码) 从后台传到前台的数据乱码(显示在页面的数据乱码) ...

  9. Linux 目录配置标准:FHS

    目录 应放置内容 /bin 和/user/目录下的/bin/都是用来保存的系统命令 /sbin 和/user/目录下的/sbin是用来保存root的系统命令 /boot 这个目录主要放置开机所用的文件 ...

  10. Cracking The Coding Interview 3.5

    //Implement a MyQueue class which implements a queue using two stacks. #include <iostream> #in ...