题目 

不使用任何内建的哈希表库设计一个哈希映射

具体地说,你的设计应该包含以下的功能

  • 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的更多相关文章

  1. Leetcode706.Design HashMap设计哈希映射

    不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get(key ...

  2. 【Leetcode_easy】706. Design HashMap

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

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

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

  4. 706. Design HashMap - LeetCode

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

  5. [leetcode] 706. Design HashMap

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

  6. [Swift]LeetCode706. 设计哈希映射 | Design HashMap

    Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...

  7. [LeetCode] Design HashMap 设计HashMap

    Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...

  8. LeetCode 706 Design HashMap 解题报告

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

  9. [LeetCode&Python] Problem 706. Design HashMap

    Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...

随机推荐

  1. 性能测试工具LoadRunner11-LR之Virtual User Generator 移动app录制

    准备条件: 1.安装插件LR_03105_Patch4.EXE,安装完成之后就会有Mobile App(HTTP/HTML),如下截图所示 2.安装热点wifi,160wifi(注:有可能有的热点软件 ...

  2. python单元测试框架-unittest(五)之跳过测试和预期失败

    概要 @unittest.skip(reason): skip(reason)装饰器:直接跳过测试,并说明跳过测试的原因. @unittest.skipIf(reason): skipIf(condi ...

  3. [转]JQuery ui 实现类似于confirm的功能

    本文转自:http://www.cnblogs.com/JerryWang1991/archive/2011/08/04/2127503.html 今天在改进参加一个全国比赛的项目作品时,发现使用了大 ...

  4. Spring Cloud学习笔记之微服务架构

    目录 什么是微服务 架构优点 架构的挑战 设计原则 什么是微服务     微服务构架方法是以开发一种小型服务的方式,来开发一个独立的应用系统的.     其中每个小型服务都运行在自己的进程中,并经常采 ...

  5. 05-spring整合jdbc-jdbc模板对象JdbcTemplate

    1 演示JdbcTemplate模板对象 1 导包 2 准备数据库 3 书写UserDao package www.test.dao; import java.util.List; import ww ...

  6. Actor的一生

    Actor应该怎么去形容它呢?它是一段代码扮演的角色.它拥有自己的状态机,能根据外界的消息进行适当的反应.他有记忆能力,可以记住来自外界的多个消息并依次进行反应.Actor就像一个小的生命体,有自己的 ...

  7. C#图表控件ZedGraph使用

    最近从java转到C#下开发PC端的桌面程序,之前也尝试用java GUI写桌面程序,发现java写桌面程序还是诸多不便变,虽然最后也写出来了,但是决心还是另起平台,有了一定的java基础,来学习C# ...

  8. hibernate课程 初探一对多映射2-5 创建持久化类并配置映射文件

    学习点: 1 一对多映射,一方 Grade.hbm.xml的写法: <hibernate-mapping> <class name="com.ddwei.entity.Gr ...

  9. Unable to copy a file from obj\Debug to bin\Debug

    1. Exit the VS2012, and then re-open the solution. 2. Clean the solution and build.

  10. Eclipse+ADT+Android SDK 搭建安卓开发环境(转)

    要求 必备知识 windows 7 基本操作. 运行环境 windows 7(64位); eclipse-jee-luna-SR2-win32(32位);ADT-23.0.4 下载地址 环境下载 最近 ...