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 ...
随机推荐
- 性能测试工具Jmeter05-badboy检查点与参数化
检查点设置 选择要检查的文字,然后在Tools->Add Assertion for Selection里添加断言,在进行回放 参数化 在请求搜索关机键字的部分进行参数化 选中搜索关键字- ...
- 当前activity透明度的获取与修改
WindowManager.LayoutParams lp = getWindow().getAttributes();//layoutparams是静态类不能通过new来完成. lp.alpha = ...
- PopUpWindow使用方法
个人使用建议,容易犯错:先设置属性再显示,而不是先出来了,再设置都没用了,显示一般是用showatlocation,或者showasdropdown 个人建议2:popupWindow的显示的两个方法 ...
- Python collections
#count对象 Only 2.7 from collections import Counter #统计字母出现的次数 Counter('hello world') Counter(['red', ...
- Spring boot-(3) Spring Boot特性1
本节将深入Spring Boot的细节,可以学到你想使用的或定制的Spring Boot的主要特性. 1. SpringApplication SpringApplication类为引导一个Sprin ...
- 使用dtd--属性声明
<!ATTLIST 元素名 属性名称 属性类型 属性特点> 1.属性类型 类型 含义 CDATA 纯文本 enumerated 枚举类型 ID 以属性的方式唯一标识改元素,必须以字母开头 ...
- 用IDEA创建一个SpringBoot项目
next后等待项目构建完成 运行方法一: 方法二:
- Python数据类型及常用操作
Python字符串类型 1.用途: 用来记录有描述性的状态.比如:人名,地址等. 2.定义方式: 创建字符串非常简单,在‘ ’,“ ”,‘’‘ ’‘’内一填写一系列的字符例如:msg='hello' ...
- Java Knowledge series 4
JVM & Bytecode Has-a or Is-a relationship(inheritance or composition) 如果想利用新类内部一个现有类的特性,而不想使用它的接 ...
- ChromiumFX
序言 开发C#桌面应用程序有很多选择,比如WinForm或WPF. 这里我们用ChromiumFX. 目录 一.Centos7 从零编译Nginx+PHP+MySql 二.Centos7 从零配置Ng ...