LeetCode706. Design HashMap
题目
不使用任何内建的哈希表库设计一个哈希映射
具体地说,你的设计应该包含以下的功能
put(key, value):向哈希映射中插入(键,值)的数值对。如果键对应的值已经存在,更新这个值。get(key):返回给定的键所对应的值,如果映射中不包含这个键,返回-1。remove(key):如果映射中存在这个键,删除这个数值对。
示例:
MyHashMap hashMap = new MyHashMap();
hashMap.put(1, 1);
hashMap.put(2, 2);
hashMap.get(1); // 返回 1
hashMap.get(3); // 返回 -1 (未找到)
hashMap.put(2, 1); // 更新已有的值
hashMap.get(2); // 返回 1
hashMap.remove(2); // 删除键为2的数据
hashMap.get(2); // 返回 -1 (未找到)
注意:
- 所有的值都在
[1, 1000000]的范围内。 - 操作的总数目在
[1, 10000]范围内。 - 不要使用内建的哈希库。
考点
思路
跟hashset的区别就是hashmap的初值为-1,重置后的值为value
代码
solution1
class MyHashMap {
public:
/** Initialize your data structure here. */
MyHashMap() {
data.resize(1000000, -1);
}
/** value will always be non-negative. */
void put(int key, int value) {
data[key] = value;
}
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
int get(int key) {
return data[key];
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
void remove(int key) {
data[key] = -1;
}
private:
vector<int> data;
};
solution2
class MyHashMap {
public:
/** Initialize your data structure here. */
MyHashMap() {
data.resize(1000,vector<int>());
}
/** value will always be non-negative. */
void put(int key, int value) {
int hashkey=key%1000;
if(data[hashkey].empty())
{
data[hashkey].resize(1000,-1);//这里分配-1
}
data[hashkey][key/1000]=value;
}
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
int get(int key) {
int hashkey=key%1000;
if(data[hashkey].empty())
{
return -1;
}
return data[hashkey][key/1000];
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
void remove(int key) {
int hashkey=key%1000;
if(!data[hashkey].empty())
{
data[hashkey][key/1000]=-1;
}
}
private:
vector<vector<int>> data;
};
/**
* Your MyHashMap object will be instantiated and called as such:
* MyHashMap obj = new MyHashMap();
* obj.put(key,value);
* int param_2 = obj.get(key);
* obj.remove(key);
*/
问题
LeetCode706. Design HashMap的更多相关文章
- Leetcode706.Design HashMap设计哈希映射
不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get(key ...
- 【Leetcode_easy】706. Design HashMap
problem 706. Design HashMap solution1: class MyHashMap { public: /** Initialize your data structure ...
- 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] 706. Design HashMap
题目 Design a HashMap without using any built-in hash table libraries. Implement the MyHashMap class: ...
- [Swift]LeetCode706. 设计哈希映射 | Design HashMap
Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...
- [LeetCode] Design HashMap 设计HashMap
Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...
- LeetCode 706 Design HashMap 解题报告
题目要求 Design a HashMap without using any built-in hash table libraries. To be specific, your design s ...
- [LeetCode&Python] Problem 706. Design HashMap
Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...
随机推荐
- (转)Python字典实现三级菜单
Python字典实现三级菜单 原文:https://www.cnblogs.com/pyramid1001/p/5803294.html 1 ############################# ...
- MATLAB矩阵求值和稀疏矩阵
方阵的行列式: det(A) 矩阵线性无关的行数或列数,称为矩阵的秩. rank(A) 求3~20阶魔方矩阵的秩 for n=3:20 rank(magic(n)) end 矩阵的迹等于矩阵的对角线元 ...
- 【转】Android 中的 Service 全面总结
1.Service的种类 按运行地点分类: 类别 区别 优点 缺点 应用 本地服务(Local) 该服务依附在主进程上, 服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另 ...
- 1.C#中的注释符
1.软件行业的道德规范 (1).程序员在日常写代码的过程中,一定要养成注释的好习惯,方便后面对理解和使用. (2).在给标识符命名的时候一定要规范,有理有据的,名字不能瞎写. 2.注释 注释符的作用: ...
- 总结spring
通过对spring的学习 什么是spring Spring是一个基于IOC和AOP的结构J2EE系统的框架 IOC 反转控制 是Spring的基础,Inversion Of Control 简单说就是 ...
- Python之人工智能:PyAudio 实现录音 自动化交互实现问答
Python 很强大其原因就是因为它庞大的三方库 , 资源是非常的丰富 , 当然也不会缺少关于音频的库 关于音频, PyAudio 这个库, 可以实现开启麦克风录音, 可以播放音频文件等等,此刻我们不 ...
- 关于修改test9ui布局的一些小笔记
今早,上IT修真园里,看到师兄大娃很负责任的将我任务里的项目的排版,3,6,7的列了出来. 谢谢师兄,那么负责任的照看师弟. 言归正传,我一开始,直接按照师兄的指示,选择性的优先修改底部.效果也达到了 ...
- oracle多表查询和子查询练习
--1.列出至少有三个员工的所有部门和部门信息. SELECT D.DEPTNO, D.DNAME, D.LOC, T.COUNTS FROM DEPT D, (SELECT DEPTNO, CO ...
- RDF类型报表-PDF中文乱码
在Oracle R12中,遇到了客户一张客户化的报表: 报表的输出格式是布局在RDF文件(非RTF)中,在并发请求输出时,PDF会出现中文乱码,而HTML和excel显示正常: 根据资料: 查看$OA ...
- 【起航计划 021】2015 起航计划 Android APIDemo的魔鬼步伐 20 App->Intents createChooser
Intents 这个例子的代码非常简单: public void onGetMusic(View view) { Intent intent = new Intent(Intent.ACTION_GE ...