Design a HashMap without using any built-in hash table libraries.html
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:app
[0, 1000000]
.[1, 10000]
.
这道题让咱们设计一个HashMap的数据结构,不能使用自带的哈希表,跟以前那道Design HashSet很相似,以前那道的两种解法在这里也是行得通的,既然题目中说了数字的范围不会超过1000000,那么咱们就申请这么大空间的数组,只需将数组的初始化值改成-1便可。空间足够大了,咱们就能够直接创建映射,移除时就将映射值重置为-1,因为默认值是-1,因此获取映射值就能够直接去,参见代码以下:post
解法一:优化
class MyHashMap { public: /** Initialize your data structure here. */ MyHashMap() { data.resize(1000000, -1); } /** value will always be non-negative. */ void put(int key, int value) { data[key] = value; } /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */ int get(int key) { return data[key]; } /** Removes the mapping of the specified value key if this map contains a mapping for the key */ void remove(int key) { data[key] = -1; } private: vector<int> data; };
咱们能够来适度的优化一下空间复杂度,因为存入HashMap的映射对儿也许不会跨度很大,那么直接就申请长度为1000000的数组可能会有些浪费,那么咱们其实可使用1000个长度为1000的数组来代替,那么就要用个二维数组啦,实际上开始咱们只申请了1000个空数组,对于每一个要处理的元素,咱们首先对1000取余,获得的值就看成哈希值,对应咱们申请的那1000个空数组的位置,在创建映射时,一旦计算出了哈希值,咱们将对应的空数组resize为长度1000,而后根据哈希值和key/1000来肯定具体的加入映射值的位置。获取映射值时,计算出哈希值,若对应的数组不为空,直接返回对应的位置上的值。移除映射值同样的,先计算出哈希值,若是对应的数组不为空的话,找到对应的位置并重置为-1。参见代码以下:ui
解法二:this
class MyHashMap { public: /** Initialize your data structure here. */ MyHashMap() { data.resize(1000, vector<int>()); } /** value will always be non-negative. */ void put(int key, int value) { int hashKey = key % 1000; if (data[hashKey].empty()) { data[hashKey].resize(1000, -1); } data[hashKey][key / 1000] = value; } /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */ int get(int key) { int hashKey = key % 1000; if (!data[hashKey].empty()) { return data[hashKey][key / 1000]; } return -1; } /** Removes the mapping of the specified value key if this map contains a mapping for the key */ void remove(int key) { int hashKey = key % 1000; if (!data[hashKey].empty()) { data[hashKey][key / 1000] = -1; } } private: vector<vector<int>> data; };
相似题目:url
参考资料:
https://leetcode.com/problems/design-hashmap
https://leetcode.com/problems/design-hashmap/discuss/152746/Java-Solution