[LeetCode] Design HashMap 設計HashMap

 

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

    • 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,因此獲取映射值就能夠直接去,參見代碼以下: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

Design HashSetspa

 

參考資料:

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 題目講解彙總(持續更新中...)

相關文章
相關標籤/搜索