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. 数据类型(C++)

    不同系统会有不同差异: 类型 位(byte) 范围 char 1 -128—127 or 0 – 255 unsigned char 1 0 – 255 signed int 1 -128—127 i ...

  2. Python3 Address already in use 解决方法

    1.查看使用端口号netstat -ntlp 2.根据端口号找到pid 3.杀死程序 kill -9 pid 4.重新启动程序 简单粗暴 我使用python3时编写Socket,linux系统下使用c ...

  3. codeforces#1157D. Ehab and the Expected XOR Problem(构造)

    题目链接: http://codeforces.com/contest/1174/problem/D 题意: 构造一个序列,满足以下条件 他的所有子段的异或值不等于$x$ $1 \le a_i< ...

  4. linux环境中关闭tomcat,通过shutdown.sh无法彻底关闭--线程池

    最近测试环境上测试的项目通过shutdown.sh始终无法彻底关闭. 之前临时解决方法两种: 第一:通过ps -ef|grep tomcat查看到tomcat的进程直接使用kill来杀死进程. 第二: ...

  5. docker crontab踩坑记录

    环境,docker centos7.4 容器启动时注意两点 入口要设置/usr/sbin/init,并且配置主机完全访问权限(--privileged) (否则执行service的时候会出现Faile ...

  6. Fiddler主界面图标简单说明

    Fiddler主界面图标简单说明: 名称 含义 # 抓取HTTP Request的顺序,从1开始,以此递增 Result HTTP状态码 Protocol 请求使用的协议,如HTTP/HTTPS/FT ...

  7. webpack入门-配置项

    一.常用配置 1.enter(表示入口,webpack从此处开始构建) 2.output(配置输出结果) 3.module(关于模块的配置,内部可以配置loader) 4.resolve(配置寻找模块 ...

  8. ThinkPHP空操作与命名空间

    命名空间:相当于一个虚拟的目录 正常管理文件使用文件夹--物理区分 TP框架的初始命名空间是:ThinkPHP\Library 在TP框架下命名空间里面使用\代表的是初始命名空间(ThinkPHP\L ...

  9. vue-cli3.0 初体验

    vue-cli3.0 自我记录 其实在2018年8月10号,vue-cli3.0就已经面世了,由于项目中应用的全是2.x版本,所以并不了解3.0的vue-cli发生了什么变化,那今天尝试了下遇见的问题 ...

  10. [转]Nginx实现高并发的原理

    Nginx 首先要明白,Nginx 采用的是多进程(单线程) & 多路IO复用模型.使用了 I/O 多路复用技术的 Nginx,就成了”并发事件驱动“的服务器. 异步非阻塞(AIO)的详解ht ...