Design HashMap
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的更多相关文章
- 【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 ...
- LeetCode 706:设计哈希映射 Design HashMap
题目: 不使用任何内建的哈希表库设计一个哈希映射 具体地说,你的设计应该包含以下的功能 put(key, value):向哈希映射中插入(键,值)的数值对.如果键对应的值已经存在,更新这个值. get ...
随机推荐
- andSelf() V1.2 加入先前所选的加入当前元素中
andSelf() V1.2概述 加入先前所选的加入当前元素中 对于筛选或查找后的元素,要加入先前所选元素时将会很有用.直线电机生产厂家 从jQuery1.8开始,.andSelf()方法已经被标注过 ...
- 路由器配置——路由重分布1(rip)
一.实验目的:使用路由重分布达到全网互通 二.拓扑图: 三.具体实验步骤配置 先给各个主机配置ip地址和网关以PC1为例: (1)R1路由器配置 Router>enable --进入特权模式R ...
- python sqlite3查询表记录
1.查询数据库log_info表最后10条记录. desc - 指"降序" 解决的方法是:按照ID 逆序排列,选取前10个 select * from 'log_info' ord ...
- ACM之路(17)—— 博弈论
博弈论这方面网上资料庞大,我觉得我不可能写的比他们好,就转载一下我觉得写的不错的博客好了. 首先是三大博弈:巴什博奕,威佐夫博奕,尼姆博奕.博客:三大基本博弈. 然后是强大的sg函数和sg定理:SG. ...
- python 二进制加法
bin(int(a,2)+int(b,2))[2:]
- shell脚本编程进阶
在linux shell中,通常我们将一些命令写在一个文件中就算是一个shell脚本了,但是如果需要执行更为复杂的逻辑判断,我们就需要使用流程控制语句来支持了.所谓流程控制既是通过使用流程控制语句对程 ...
- LeetCode 132. 分割回文串 II(Palindrome Partitioning II)
题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一次分割就可将 s ...
- docker mysql容器的安装使用
docker拉取镜像的命令为 docker pull image[:TAG] 如果我们想安装的mysql版本不是最新的版本,则需要填充TAG,如果不带上TAG,则默认安装的为最新版本,虽然可以通过do ...
- 基于角色的权限控制系统(role-based access control)
role-based access control(rbac),指对于不同角色的用户,拥有不同的权限 .用户对应一个角色,一个角色拥有若干权限,形成用户-角色-权限的关系,如下图所示.当一个用户进行访 ...
- rest-assured学习资料
rest-assured 使用指南 https://blog.csdn.net/wx19900503/article/details/54944841/