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. pandas dataframe 过滤——apply最灵活!!!

    按照某特定string字段长度过滤: import pandas as pd df = pd.read_csv('filex.csv') df['A'] = df['A'].astype('str') ...

  2. js正则表达式 replace替换url的参数

    /* 定义替换对象键值 */var setReferArgs = function(){    var referArgs = new Object();    referArgs['#userID\ ...

  3. Oracle修改监听端口教程

    Oracle默认监听端口1521,一众扫描器通常通过探测1521端口是否开启来探测是否存在Oracle服务,如果修改默认监听端口在一定程度上可以提升数据库和主机的安全性. 比如这里我们修改成2521为 ...

  4. Sql server2008如何导入Excel文件数据?

    sql server 中如何使用Excel文件导入数据?我做个测试,首先建立一个测试表(民族表) --创建一个民族表-- create table BdsNation( Uid int not nul ...

  5. linux用户管理 用户和用户组信息

    用户管理配置文件 用户信息文件  /etc/passwd 密码文件 /etc/shadow 用户配置文件 /etc/login.defs /etc/default/useradd 新用户信息文件 /e ...

  6. jquery for&&each的用法

    var arr=['姚明','易建联','张继科']: each&&for用法 $.each(arr,function(index,value){ document.write(ind ...

  7. MAVEN 创建 JAR项目

    从Maven模板创建一个项目 在终端或命令提示(windows)中,浏览到要创建JAVA项目的文件夹下 键入如下命令: mvn archetype:generate -DgroupId={projec ...

  8. 为什么使用 npm Scripts 构建项目

    http://www.css88.com/archives/7025#more-7025 https://github.com/damonbauer/npm-build-boilerplate 这个我 ...

  9. JavaScript -基础- 函数与对象

    一.JavaScript三对象 1.分类方式一 1)ECMAScript JavaScript的ECMA规范 JS本身的对象 2)Dom 操作HTML相关 3)BOM游览器对象 游览器窗口对象,全局的 ...

  10. python中字典常用的方法

    #定义一个空字典: a={ } 定义一个字典: d={'age':18} #增加一个元素: d['age']=20   d[k]=v d.setdefault('age',18)    d.setde ...