题目:
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)
Note:
- All keys and values will be in the range of
[0, 1000000]
. - The number of operations will be in the range of
[1, 10000]
. - Please do not use the built-in HashMap library.
为了简单,采用了open addressing chaining设计collision处理方式,table直接是array of list,每个list里面存相同index的所有entry。对,还要自己写一个entry类,list里面要存entry(包括key和value),不能只存key or value。另外就是注意一些initialize的小细节。没啥难度。
Runtime: 17 ms, faster than 70.30% of Java online submissions for Design HashMap.
Memory Usage: 42.9 MB, less than 92.49% of Java online submissions for Design HashMap.
class MyHashMap {
private class Entry {
int key;
int value;
Entry(int key, int value) {
this.key = key;
this.value = value;
}
}
int size;
int capacity = 101;
List<Entry>[] table;
/** Initialize your data structure here. */
public MyHashMap() {
table = new ArrayList[capacity];
for (int i = 0; i < capacity; i++) {
table[i] = new ArrayList<>();
}
}
/** value will always be non-negative. */
public void put(int key, int value) {
int index = getIndex(key);
if (table[index].size() == 0) {
table[index].add(new Entry(key, value));
} else {
remove(key);
table[index].add(new Entry(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) {
int index = getIndex(key);
if (table[index].size() == 0) {
return -1;
} else {
for (Entry entry : table[index]) {
if (entry.key == key) {
return entry.value;
}
}
}
return -1;
}
/** 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);
Entry toRemove = null;
if (table[index].size() == 0) {
return;
} else {
for (Entry entry : table[index]) {
if (entry.key == key) {
toRemove = entry;
break;
}
}
}
if (toRemove != null) {
table[index].remove(toRemove);
}
}
private int getIndex(int key) {
return key % capacity;
}
}
/**
* Your MyHashMap object will be instantiated and called as such:
* MyHashMap obj = new MyHashMap();
* obj.put(key,value);
* int param_2 = obj.get(key);
* obj.remove(key);
*/
2022.11.6
这次没有用built in LinkedList,自己写了个ListNode class来存数据。于是这个ListNode class需要存放key, value和next指针。这里就没有再考虑load factor和rehash了,详情见hashset题(LeetCode 705. Design HashSet_wenyq7的博客-CSDN博客)。写完以后发现还是WA了,debug了半小时才发现是remove head的时候直接把这个index对应的list设为null了,哎。另外看了discussion发现大家在往list里加node的时候直接加在了头部,就不需要遍历一次list了,其实也是个更高效的做法(which was also discussed on that interview which got me here),写起代码来可以搭配remove使用,先给remove掉再put,代码就更简洁了。
自己写的:
class ListNode {
int key, val;
ListNode next;
public ListNode(int key, int val, ListNode next) {
this.key = key;
this.val = val;
this.next = next;
}
}
class MyHashMap {
ListNode[] array;
int capacity = 80;
public MyHashMap() {
array = new ListNode[capacity];
}
public void put(int key, int value) {
ListNode node = array[getHash(key)];
if (node == null) {
array[getHash(key)] = new ListNode(key, value, null);
} else {
ListNode prev = null;
while (node != null) {
if (node.key == key) {
node.val = value;
return;
}
prev = node;
node = node.next;
}
prev.next = new ListNode(key, value, null);
}
}
public int get(int key) {
ListNode node = array[getHash(key)];
while (node != null) {
if (node.key == key) {
return node.val;
}
node = node.next;
}
return -1;
}
public void remove(int key) {
ListNode node = array[getHash(key)];
ListNode prev = null;
while (node != null) {
if (node.key == key) {
if (prev == null) {
array[getHash(key)] = node.next; // where I debugged for a long time
} else {
prev.next = node.next;
}
return;
}
prev = node;
node = node.next;
}
}
private int getHash(int key) {
return key % capacity;
}
}
/**
* Your MyHashMap object will be instantiated and called as such:
* MyHashMap obj = new MyHashMap();
* obj.put(key,value);
* int param_2 = obj.get(key);
* obj.remove(key);
*/
put改成往头上put和搭配remove使用:
class ListNode {
int key, val;
ListNode next;
public ListNode(int key, int val, ListNode next) {
this.key = key;
this.val = val;
this.next = next;
}
}
class MyHashMap {
ListNode[] array;
int capacity = 80;
public MyHashMap() {
array = new ListNode[capacity];
}
public void put(int key, int value) {
remove(key);
array[getHash(key)] = new ListNode(key, value, array[getHash(key)]);
}
public int get(int key) {
ListNode node = array[getHash(key)];
while (node != null) {
if (node.key == key) {
return node.val;
}
node = node.next;
}
return -1;
}
public void remove(int key) {
ListNode node = array[getHash(key)];
ListNode prev = null;
while (node != null) {
if (node.key == key) {
if (prev == null) {
array[getHash(key)] = node.next;
} else {
prev.next = node.next;
}
return;
}
prev = node;
node = node.next;
}
}
private int getHash(int key) {
return key % capacity;
}
}
/**
* Your MyHashMap object will be instantiated and called as such:
* MyHashMap obj = new MyHashMap();
* obj.put(key,value);
* int param_2 = obj.get(key);
* obj.remove(key);
*/