题目要求

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. 机器学习&深度学习基础(目录)

    从业这么久了,做了很多项目,一直对机器学习的基础课程鄙视已久,现在回头看来,系统的基础知识整理对我现在思路的整理很有利,写完这个基础篇,开始把AI+cv的也总结完,然后把这么多年做的项目再写好总结. ...

  2. 解决com.mysql.jdbc.PacketTooBigException: Packet for query is too large

    在做查询数据库操作时,报了以上错误,还有out of memery heap hacp ,原因是MySQL的max_allowed_packet设置过小引起的,我一开始设置的是1M,后来改为了20M ...

  3. Java 读取ANSI文件中文乱码问题解决方式[转]

    第一步:首先判断源文件的编码格式: 按照给定的字符集存储文件时,在文件的最开头的三个字节中就有可能存储着编码信息,所以,基本的原理就是只要读出文件前三个字节,判定这些字节的值,就可以得知其编码的格式. ...

  4. Asp.net常用的三十多个代码(非常实用)

    1.//弹出对话框.点击转向指定页面 Response.Write("<script>window.alert('该会员没有提交申请,请重新提交!')</script> ...

  5. 简单的SSM框架搭建教程

    简单的ssm框架的搭建和配置文件 ssm框架里边的配置: 1.src路径下直接存放数据库和log4j的properties文件 2.src路径下建个config包,分别放置ssm的xml文件 3.修改 ...

  6. java程序员必须要学会的linux命令总结

    1.查找文件find / -name filename.txt 根据名称查找/目录下的filename.txt文件.find . -name “*.xml” 递归查找所有的xml文件2.查看一个程序是 ...

  7. "佛祖保佑 永无bug" 注释模板设置详解(仅供娱乐)

    1.注释模板效果图 今天在网上看到一段有趣的注释,佛祖保佑 永无bug, 效果如下图所示:  代码如下所示: /** * _ooOoo_ * o8888888o * 88" . " ...

  8. Java基础学习(一)---Java初识

    一.Java介绍 关于Java的诞生和发展网上比较多,在此就不再赘述了,可以参考http://i.cnblogs.com/EditArticles.aspx?postid=4050233. 1.1 J ...

  9. js函数作用域

    函数 1.函数没有用return返回函数时,返回默认参数undefined 结果 return返回得话 就是里面得数值 结果 JS执行过程是上到下,下面的a元素覆盖了上面的a元素 function d ...

  10. [原]openstack-kilo--issue(九) heat stacks topology中图形无法正常显示

    本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. ======声明======= 欢迎转载:转载请注明出处 http://www. ...