LeetCode 706 Design HashMap 解题报告
题目要求
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 解题报告的更多相关文章
- 【LeetCode】706. Design HashMap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [leetcode] 706. Design HashMap
题目 Design a HashMap without using any built-in hash table libraries. Implement the MyHashMap class: ...
- LeetCode 706. Design HashMap (设计哈希映射)
题目标签:HashMap 题目让我们设计一个 hashmap, 有put, get, remove 功能. 建立一个 int array, index 是key, 值是 value,具体看code. ...
- LeetCode 705 Design HashSet 解题报告
题目要求 Design a HashSet without using any built-in hash table libraries. To be specific, your design s ...
- Leetcode PHP题解--D75 706. Design HashMap
2019独角兽企业重金招聘Python工程师标准>>> D75 706. Design HashMap 题目链接 706. Design HashMap 题目分析 自行设计一个has ...
- 706. Design HashMap - LeetCode
Question 706. Design HashMap Solution 题目大意:构造一个hashmap 思路:讨个巧,只要求key是int,哈希函数选择f(x)=x,规定key最大为100000 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【Leetcode_easy】706. Design HashMap
problem 706. Design HashMap solution1: class MyHashMap { public: /** Initialize your data structure ...
随机推荐
- 《Essential C++》读书笔记 之 泛型编程风格
<Essential C++>读书笔记 之 泛型编程风格 2014-07-07 3.1 指针的算术运算(The Arithmetic of Pointer) 新需求1 新需求2 新需求3 ...
- masscan
masscan是一个快速的端口扫描工具 大概说一下它的使用方法,既有原创也有翻译 欢迎补充 扫描10.x.x.x的网络:masscan 10.0.0.0/8 -p80 程序将自动探测网络的接口和适配器 ...
- 很好的git教程
http://www.liaoxuefeng.com/这里的git教程很好,我是从这入的门.
- centos7 网络配置
vi /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPR ...
- jxl和POI的区别
最近两个项目中分别用到jxl和POI,因为用的都是其中的简单的功能,所以没有觉得这其中有太大的区别.有人针对他们做了比较,这里也拿出来展示一下. 首先从优缺点上来说 一.jxl 优点: Jxl对中文支 ...
- mybatis通用mapper源码解析(二)
1.javabean的属性值生成sql /** * 获取所有查询列,如id,name,code... * * @param entityClass * @return */ public static ...
- asp.net mvc 3.0 知识点整理 ----- (3).HtmlHelper(Html 辅助方法)介绍
在View视图中,Html的类型是System.Web.Mvc.HtmlHelper<T>, 所有的辅助方法都需要和ModelState交互.那么,ModelState是什么呢?它是模型绑 ...
- Nuxt.js项目实战
感悟 经过几个周六周日的尝试,终于解决了服务端渲染中的常见问题,当SEO不在是问题的时候,或许才是我们搞前端的真正的春天,其中也遇到了一些小坑,Nuxt.js官方还是很给力的,提issue后很积极的给 ...
- Qt编写守护程序保证程序一直运行(开源)
没有任何人敢保证自己写的程序没有任何BUG,尤其是在商业项目中,程序量越大,复杂度越高,出错的概率越大,尤其是现场环境千差万别,和当初本地电脑测试环境很可能不一样,有很多特殊情况没有考虑到,如果需要保 ...
- ASP.NET MVC与ASP.NET Web Form简单区别
概论: Asp.net 微软 提供web开发框架或者技术.分Web Form和ASP.NET MVC.下面简单说明各自优缺点及使用场景. Web Form 优点: 1.支持丰富的服务器控件.如:Gr ...