不使用任何内建的哈希表库设计一个哈希映射(HashMap)。
实现 MyHashMap 类:
MyHashMap() 用空映射初始化对象
void put(int key, int value) 向 HashMap 插入一个键值对 (key, value) 。如果 key 已经存在于映射中,则更新其对应的值 value 。
int get(int key) 返回特定的 key 所映射的 value ;如果映射中不包含 key 的映射,返回 -1 。
void remove(key) 如果映射中存在 key 的映射,则移除 key 和它所对应的 value 。
示例:
输入:
["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
输出:
[null, null, null, 1, -1, null, 1, null, -1]
解释:
MyHashMap myHashMap = new MyHashMap();
myHashMap.put(1, 1); // myHashMap 现在为 [[1,1]]
myHashMap.put(2, 2); // myHashMap 现在为 [[1,1], [2,2]]
myHashMap.get(1); // 返回 1 ,myHashMap 现在为 [[1,1], [2,2]]
myHashMap.get(3); // 返回 -1(未找到),myHashMap 现在为 [[1,1], [2,2]]
myHashMap.put(2, 1); // myHashMap 现在为 [[1,1], [2,1]](更新已有的值)
myHashMap.get(2); // 返回 1 ,myHashMap 现在为 [[1,1], [2,1]]
myHashMap.remove(2); // 删除键为 2 的数据,myHashMap 现在为 [[1,1]]
myHashMap.get(2); // 返回 -1(未找到),myHashMap 现在为 [[1,1]]
其实今天的题和昨天基本一样,首先也是使用大数据法,就是多加了个数组。
package isLeetcode;
import java.util.Map;
public class MyHashMap314 {
boolean[] key = new boolean[1000001];
int[] value = new int[1000001];
/** Initialize your data structure here. */
public MyHashMap314() {
}
/** value will always be non-negative. */
public void put(int key, int value) {
this.key[key]=true;
this.value[key]=value;
}
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
public int get(int key) {
if(this.key[key]==true){
return this.value[key];
}else return -1;
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
public void remove(int key) {
this.key[key]=false;
this.value[key]=0;
}
}
由于对Node 以及 Pair 没有具体的了解,在此先贴上leetCode大佬的解法,留已学习。
//作者:cap
//链接:https://leetcode-cn.com/problems/design-hashmap/solution/xiang-jie-hashmap-de-she-ji-zai-shi-jian-85k9/
//来源:力扣(LeetCode)
class MyHashMap {
Node[] array;
int size;
/** Initialize your data structure here. */
public MyHashMap() {
array = new Node[1<<15];//make sure size is powers of 2
size =0;
}
/** value will always be non-negative. */
public void put(int key, int value) {
//factor is 0.75
if(++size>array.length*0.75){
//double size
resize();
}
int index = getIndex(key,array.length);
Node head = array[index];
Node cur = new Node(key,value);
if(head ==null){// head has not been initialized
head = new Node();
array[index] = head;
head.next = cur;
}else{
//cover the old value
Node h2 = head.next;
while(h2!=null){
if(h2.key == key){
h2.value = value;
return;
}
h2 = h2.next;
}
h2 = head.next;
head.next = cur;
cur.next = h2;
}
}
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
public int get(int key) {
int index = getIndex(key,array.length);
Node head = array[index];
if(head ==null){
return -1;//not found
}else{
Node h2 = head.next;
while(h2!=null){
if(h2.key == key){
return h2.value;
}
h2 = h2.next;
}
return -1;//not found
}
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
public void remove(int key) {
int index = getIndex(key,array.length);
Node head = array[index];
if(head==null){//not found
return;
}else{
Node pre = head;
Node h2 = head.next;
while(h2!=null){
if(h2.key!=key){
pre = h2;
h2 = h2.next;
}else{
pre.next = h2.next;
size--;
return;
}
}
}
}
private void resize(){
//double size
Node[] array2 = new Node[array.length<<1];
//transfer node from old bin into new bin
for(int i=0;i<array.length;i++){
Node head1 = array[i];
if(head1!=null){
Node h1 = head1.next;
while(h1!=null){
Node next = h1.next;
int key = h1.key;
int newIndex = getIndex(key,array2.length);
Node head2 = array2[newIndex];
if(head2==null){
head2 = new Node();
head2.next = h1;
array2[newIndex]=head2;
}else{
Node temp = head2.next;
head2.next = h1;
h1.next = temp;
}
h1 = next;
}
}
}
array = array2;
}
//make sure size is powers of 2
//then key%size = key&(size-1)
private int getIndex(int key,int size){
return key&(size-1);
}
class Node{
int key;
int value;
Node next;
public Node(int key,int value){
this.key = key;
this.value = value;
}
public Node(){
}
}
}
此文章创于本人学习时的记录,如有错误或更优解还请指出。