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 ...
 
随机推荐
- 牛客网Java刷题知识点之匿名对象、匿名对象的内存结构图、匿名对象的应用场景、匿名对象的使用、匿名对象的简单例子、匿名对象要注意的事项
			
不多说,直接上干货! 什么是匿名对象? 答: 没有名字的实体,也就是该实体没有对应的变量名引用. 没有名字的实体,没有引用类型变量指向的对象称作为匿名对象. 正常的,是 Car car = new ...
 - 计算机为什么要区别C盘,D盘,E盘等?
			
为什么要区分C盘,D盘,E盘,F盘? 1)各盘出现背景 在计算机刚诞生的年代,还没有硬盘,那时数据存储主要靠软盘.软盘驱动器按照顺序占据了A和B盘符的位置,后来随着硬盘的应用,就出现了C盘及以后的 ...
 - Zend Optimizer安装、配置
			
Zend Optimizer用优化代码的方法来提高php应用程序的执行速度.实现的原理是对那些在被最终执行之前由运行编译器(Run-Time Compiler)产生的代码进行优化.这里,我们下载最新版 ...
 - Ubuntu15.10下安装Docker
			
1.首先查看linux系统版本 head -n 1 /etc/issue 2.升级包管理器 sudo apt-get update sudo apt-get install apt-transport ...
 - vue 修饰符 整理
			
事件修饰符 .stop <!-- 阻止单击事件继续传播 --> <a v-on:click.stop="doThis"></a> .preven ...
 - Django Rest Framework框架源码流程
			
在详细说django-rest-framework源码流程之前,先要知道什么是RESTFUL.REST API . RESTFUL是所有Web应用都应该遵守的架构设计指导原则. REST是Repres ...
 - node.js 模块和其下载资源的镜像设置
			
以前安装 electron 时总是失败,然后就在淘宝镜像上下载好相应版本的文件放到用户目录来解决问题. 后来研究发现 npm 不仅可以设置 node.js 模块仓库的代理, 同样可以设置像 elect ...
 - WebSocket Demo
			
HTML 代码: <body> <h1>WebScoket示例</h1> <br /><br /> <input type=" ...
 - 1像素border
			
1像素border 利用伪类和媒体查询: 伪类: border-1px($color) position:relative &:after display: block position: a ...
 - Debug get/set property
			
1. Select "Debug -> Windows -> Breakpoints" from VS menu. 2. Click "New -> B ...