[LeetCode] 706. Design HashMap 設計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) 


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.

 

這道題讓我們設計一個HashMap的數據結構,不能使用自帶的哈希表,跟之前那道 Design HashSet 很類似,之前那道的兩種解法在這里也是行得通的,既然題目中說了數字的范圍不會超過 1000000,那么就申請這么大空間的數組,只需將數組的初始化值改為 -1 即可。空間足夠大了,就可以直接建立映射,移除時就將映射值重置為 -1,由於默認值是 -1,所以獲取映射值就可以直接去,參見代碼如下:

 

解法一:

class MyHashMap {
public:
    MyHashMap() {
        data.resize(1000000, -1);
    }
    void put(int key, int value) {
        data[key] = value;
    }
    int get(int key) {
        return data[key];
    }
    void remove(int key) {
        data[key] = -1;
    }

private:
    vector<int> data;
};

 

我們可以來適度的優化一下空間復雜度,由於存入 HashMap 的映射對兒也許不會跨度很大,那么直接就申請長度為 1000000 的數組可能會有些浪費,其實可以使用 1000 個長度為 1000 的數組來代替,那么就要用個二維數組啦,實際上開始只申請了 1000 個空數組,對於每個要處理的元素,首先對 1000 取余,得到的值就當作哈希值,對應申請的那 1000 個空數組的位置,在建立映射時,一旦計算出了哈希值,將對應的空數組 resize 為長度 1000,然后根據哈希值和 key/1000 來確定具體的加入映射值的位置。獲取映射值時,計算出哈希值,若對應的數組不為空,直接返回對應的位置上的值。移除映射值一樣的,先計算出哈希值,如果對應的數組不為空的話,找到對應的位置並重置為 -1。參見代碼如下:

 

解法二:

class MyHashMap {
public:
    MyHashMap() {
        data.resize(1000, vector<int>());
    }
    void put(int key, int value) {
        int hashKey = key % 1000;
        if (data[hashKey].empty()) {
            data[hashKey].resize(1000, -1);
        } 
        data[hashKey][key / 1000] = value;
    }
    int get(int key) {
        int hashKey = key % 1000;
        if (!data[hashKey].empty()) {
            return data[hashKey][key / 1000];
        } 
        return -1;
    }
    void remove(int key) {
        int hashKey = key % 1000;
        if (!data[hashKey].empty()) {
            data[hashKey][key / 1000] = -1;
        } 
    }

private:
    vector<vector<int>> data;
};

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/706

 

類似題目:

Design HashSet

 

參考資料:

https://leetcode.com/problems/design-hashmap

https://leetcode.com/problems/design-hashmap/discuss/152746/Java-Solution

https://leetcode.com/problems/design-hashmap/discuss/184764/Easy-C%2B%2B-Solution-beats-98.01(52-msec)-using-array-of-vectors

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM