题目要求

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.

题目分析及思路

要求不使用任何内置的hash table库设计一个HashMap。具体要求是:1)put函数能将一对(key, value)插入到HashMap,若已经有value,则进行更新;2)get函数能获得对应key的value,若不存在key,则返回-1;3)remove函数能在key存在的时候去除key以及key对应的value。可以将key和value分别设置为列表,put函数里对key的存在性先进行判断,然后确定添加还是更新。get和remove函数同样需要对key的存在性先进行判断,然后前者获取对应key的索引,后者将key和与key同位置的value一起去掉。

python代码

class MyHashMap:

def __init__(self):

"""

Initialize your data structure here.

"""

self.keys = []

self.vals = []

def put(self, key: int, value: int) -> None:

"""

value will always be non-negative.

"""

if key not in self.keys:

self.keys.append(key)

self.vals.append(value)

else:

self.vals[self.keys.index(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

"""

if key in self.keys:

return self.vals[self.keys.index(key)]

else:

return -1

def remove(self, key: int) -> None:

"""

Removes the mapping of the specified value key if this map contains a mapping for the key

"""

if key in self.keys:

idx = self.keys.index(key)

self.keys.remove(key)

self.vals.pop(idx)

# 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 706 Design HashMap 解题报告的更多相关文章

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

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

  2. [leetcode] 706. Design HashMap

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

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

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

  4. LeetCode 705 Design HashSet 解题报告

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

  5. Leetcode PHP题解--D75 706. Design HashMap

    2019独角兽企业重金招聘Python工程师标准>>> D75 706. Design HashMap 题目链接 706. Design HashMap 题目分析 自行设计一个has ...

  6. 706. Design HashMap - LeetCode

    Question 706. Design HashMap Solution 题目大意:构造一个hashmap 思路:讨个巧,只要求key是int,哈希函数选择f(x)=x,规定key最大为100000 ...

  7. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  8. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  9. 【Leetcode_easy】706. Design HashMap

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

随机推荐

  1. 《Essential C++》读书笔记 之 C++编程基础

    <Essential C++>读书笔记 之 C++编程基础 2014-07-03 1.1 如何撰写C++程序 头文件 命名空间 1.2 对象的定义与初始化 1.3 撰写表达式 运算符的优先 ...

  2. PSR PHP业界规范

    0x0 大型项目的问题 随着项目越来越大,参与的人数越来越多,代码变得越来越不可维护了. 每个人都给项目带来自己的风格,所以这时就需要大家采用一个统一的标准. 0x1 解决办法 于是顶尖的PHPer们 ...

  3. 【iCore1S 双核心板_ARM】例程十三:SDIO实验——读取SD卡信息

    实验现象: 核心代码: int main(void) { /* USER CODE BEGIN 1 */ HAL_SD_TransferStateTypedef State; /* USER CODE ...

  4. 【转】JS获取浏览器可视区域的尺寸

    from: http://www.xiaoboy.com/detail/1341545044.html 所谓可视区域是指能看得见的区域,即在浏览器中能看到页面的区域(高度与宽度).刚刚使用 docum ...

  5. 【转】WPF自定义控件与样式(4)-CheckBox/RadioButton自定义样式

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等 本文主要内容: CheckBox复选框的自定义样式,有两种不同的风格实现: RadioB ...

  6. Java如何根据主机名(域名)获取IP地址?

    在Java编程中,如何根据主机名(域名)获取IP地址? 以下示例显示了如何通过net.InetAddress类的InetAddress.getByName()方法将主机名更改为指定的IP地址. pac ...

  7. jq ajax post body raw传json

    $.ajax( { url: '', 'data': JSON.stringify({ }), 'type': 'POST', 'processData': false, 'contentType': ...

  8. DOS、Mac 和 Unix 文件格式+ UltraEdit使用

    文件格式 区分DOS.Mac 和 Unix分别对应三种系统 从文件编码的方式来看,文件可分为ASCII码文件和二进制码文件两种 文件模式 区分ASCII模式和Binary模式  通常由系统决定,大多数 ...

  9. javascript实现一行文字随不同设备自适应改变字体大小至字数完全展示

    产品提了一个小需求,希望一行能展示用户输入的所有文字,因为最多限制为25字符,但是如果夹杂英文/韩文/日文等,即使字符数是一样的,但是展示的长度不一样,则有些title标题会被截断. 效果如图 前提是 ...

  10. [GPU] DIY for Deep Learning Workstation

    Link: jcjohnson/cnn-benchmarks Ref: Build a super fast deep learning machine for under $1,000 Graphi ...