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.

Example:

MyHashMap hashMap = new MyHashMap();
hashMap.put(1, 1);          
hashMap.put(2, 2);        
hashMap.get(1);            // returns 1
hashMap.get(3);            // returns -1 (not found)
hashMap.put(2, 1);          // update the existing value
hashMap.get(2);            // returns 1
hashMap.remove(2);          // remove the mapping for 2
hashMap.get(2);            // returns -1 (not found)
 class MyHashMap {
final ListNode[] nodes = new ListNode[]; public void put(int key, int value) {
int i = idx(key);
ListNode first = nodes[i];
ListNode newNode = new ListNode(key, value);
if (first == null) {
nodes[i] = newNode;
} else {
ListNode sameNode = find(nodes[i], key);
if (sameNode == null) {
newNode.next = first;
nodes[i] = newNode;
} else {
sameNode.val = value;
}
}
} public int get(int key) {
int i = idx(key);
if (nodes[i] == null) {
return -;
}
ListNode node = find(nodes[i], key);
return node == null ? - : node.val;
} public void remove(int key) {
int i = idx(key);
if (nodes[i] == null) {
return;
}
ListNode current = nodes[i];
ListNode previous = null;
while (current != null) {
if (current.key == key) {
if (previous != null) {
previous.next = current.next;
} else {
nodes[i] = current.next;
}
break;
} else {
previous = current;
current = current.next;
}
}
} int idx(int key) {
return Integer.hashCode(key) % nodes.length;
} ListNode find(ListNode node, int key) {
while (node != null) {
if (node.key == key) {
return node;
}
node = node.next;
}
return null;
} class ListNode {
int key, val;
ListNode next; ListNode(int key, int val) {
this.key = key;
this.val = val;
}
}
}
												

Design HashMap的更多相关文章

  1. 【Leetcode_easy】706. Design HashMap

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

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

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

  3. 706. Design HashMap - LeetCode

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

  4. [leetcode] 706. Design HashMap

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

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

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

  6. [LeetCode] Design HashMap 设计HashMap

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

  7. LeetCode 706 Design HashMap 解题报告

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

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

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

  9. LeetCode 706:设计哈希映射 Design HashMap

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

随机推荐

  1. Maven手动将jar导入本地仓库

    1.使用cmd进入maven安装目录下的bin 2.运行mvn install:install-file -Dfile=jar包的路径 -DgroupId=gruopId中的内容 -Dartifact ...

  2. PHP mysqli_get_client_version() 函数

    定义和用法 mysqli_get_client_version() 函数将 MySQL 客户端库版本作为整数返回. MySQL 客户端库版本将按照以下格式返回: 主要版本*10000 + 次要版本*1 ...

  3. 积性函数,线性筛入门 HDU - 2879

    HDU - 2879HeHe 题意:He[N]为[0,N−1]范围内有多少个数满足式子x2≡x (mod N),求HeHe[N]=He[1]×……×He[N] 我是通过打表发现的he[x]=2k,k为 ...

  4. 【csp模拟赛4】 珠江夜游 (cruise.cpp)-二分,贪心

    Problem 1 珠江夜游 (cruise.cpp) [题目描述] 小 Z 放假后难得来一趟广州游玩,当然要吃遍广州各路美食小吃然后再 到珠江新城看看远近闻名的小蛮腰啦!可当小 Z 一路吃吃吃以后, ...

  5. C# 父类代码动态转换子类

    百度上搜索C# 如何父类运行时转换成子类,没有得到相应答案,突然想起C# 有dynamic类型试试看结果成功了... 以后编写代码类似这样的代码 就可以删减掉了 if (en.type == EMap ...

  6. 利用Apache shiro SimpleHash 加密字符串

    1.导入包 import org.apache.shiro.crypto.hash.SimpleHash; 1 2.代码 import org.apache.shiro.crypto.hash.Sim ...

  7. elasticsearch利用head插件

    restful接口使用方法 RESTful接口URL的格式: http://localhost:9200///[] 其中index.type是必须提供的. id是可选的,不提供es会自动生成. ind ...

  8. Python,初次见面请多指教

    特点 1.可读性强: 可读性远比听上去重要的多得多.一个程序会被反复的修改,可读性强意味着让你可以在更短的时间内学习和记忆,直接提高生产率. 2.简洁,简洁,简洁: 研究证明,程序员每天可编写的有效代 ...

  9. Ubuntu使用PBIS认证

    1:下载 https://github.com/BeyondTrust/pbis-open/releases wget https://github.com/BeyondTrust/pbis-open ...

  10. Android跨进程通信广播(Broadcast)

    广播是一种被动跨进程通讯的方式.当某个程序向系统发送广播时,其他的应用程序只能被动地接收广播数据.这就象电台进行广播一样,听众只能被动地收听,而不能主动与电台进行沟通,在应用程序中发送广播比较简单.只 ...