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. Python数据类型知识点

    1.字符串 字符串常用功能 name = 'derek' print(name.capitalize()) #首字母大写 Derek print(name.count("e")) ...

  2. PHP mysqli_query() 函数

    PHP mysqli_query() 函数 定义和用法 mysqli_query() 函数执行某个针对数据库的查询. mysqli_query(connection,query,resultmode) ...

  3. 校验正确获取对象或者数组的属性方法(babel-plugin-idx/_.get)

    背景: 开发中经常遇到取值属性的时候,需要校验数值的有效性. 例如: 获取props对象里面的friends属性 props.user && props.user.friends &a ...

  4. HTML JS 弹层后底部页面禁止滚动处理

    1.打开新页面时需要禁止鼠标滚轮,禁止页面滑动: 1 2 3 4 在调用显示层时加上这句js代码就可以了: document.documentElement.style.overflow = &quo ...

  5. 两种dp模型

    两个常见模型 bzoj 4321 题意:编号为1~n的人排成一排,问有多少种排法使得任意相邻两人的编号之差不为1或-1. n<=1000 排列计数问题:考虑把数从小到大插入的过程进行dp. 设 ...

  6. JVM——内存结构

    一.程序计数器/PC寄存器 (Program Counter Registe) 用于保存当前正在执行的程序的内存地址(下一条jvm指令的执行地址),由于Java是支持多线程执行的,所以程序执行的轨迹不 ...

  7. deepin Linux 日常命令、插件、方法等汇总【更新中】

    查看系统编码: dawn@dawn-PC:~$ locale LANG=zh_CN.UTF- LANGUAGE=zh_CN LC_CTYPE="zh_CN.UTF-8" LC_NU ...

  8. 年视图,选择月份 ---bootstrap datepicker

    $.fn.datepicker.dates['cn'] = { //切换为中文显示 days: ["周日", "周一", "周二", &qu ...

  9. mac使用jadx逆向app

    安装jadx 编译安装 git clone https://github.com/skylot/jadx.git cd jadx ./gradlew dist 然后将build/jadx/bin加入到 ...

  10. appium中从activity切换到html

    问题:混合开发的app中,会有内嵌的H5页面元素,该如何进行定位操作? 解决思路:appium中的元素定位都是基于android原生控件进行元素定位,而web网页是B/S架构,两者运行环境不同需要进行 ...