LeetCode C++ 706. Design HashMap【Design/Hash Table】简单

Design a HashMap without using any built-in hash table libraries.

Implement the MyHashMap class:

  • MyHashMap() initializes the object with an empty map.
  • void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.
  • int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
  • void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.

Example 1:

Input
["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
Output
[null, null, null, 1, -1, null, 1, null, -1]

Explanation
MyHashMap myHashMap = new MyHashMap();
myHashMap.put(1, 1); // The map is now [[1,1]]
myHashMap.put(2, 2); // The map is now [[1,1], [2,2]]
myHashMap.get(1);    // return 1, The map is now [[1,1], [2,2]]
myHashMap.get(3);    // return -1 (i.e., not found), The map is now [[1,1], [2,2]]
myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value)
myHashMap.get(2);    // return 1, The map is now [[1,1], [2,1]]
myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]]
myHashMap.get(2);    // return -1 (i.e., not found), The map is now [[1,1]]

Constraints:

  • 0 <= key, value <= 106
  • At most 104 calls will be made to put, get, and remove.

Follow up: Please do not use the built-in HashMap library.

题意:不使用任何内建的哈希表库设计一个哈希映射(HashMap)。

  • 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

这道设计题有太多种解法,涉及到许多有意思的数据结构,如哈希表、二叉树、二叉搜索树、01字典树、AVL树、Splay、Treap、fhq Treap、跳表、红黑树等,慢慢更新吧。

解法1 unordered_map

class MyHashMap {
public:
    /** Initialize your data structure here. */
    unordered_map<int, int> rec;
    MyHashMap() { }
    
    /** value will always be non-negative. */
    void put(int key, int value) { rec[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) {
        if (rec.find(key) != rec.end()) return rec[key];
        return -1;
    }
    
    /** Removes the mapping of the specified value key if this map contains a mapping for the key */
    void remove(int key) { rec.erase(key); }
};

运行效率如下:

执行用时:152 ms, 在所有 C++ 提交中击败了65.45% 的用户
内存消耗:51.2 MB, 在所有 C++ 提交中击败了41.78% 的用户

解法2 01字典树

按照键的二进制形式,将值插入到字典树结点中:

class Trie01Node {
private:
    int val;
    Trie01Node *sons[2];
public:
    Trie01Node(int v) : val(v) { sons[0] = sons[1] = nullptr; }
    void insert(int d, int x) { //key,val(val非负数)
        if (d) {
            if (sons[d & 1] == nullptr) sons[d & 1] = new Trie01Node(-1);
            sons[d & 1]->insert(d >> 1, x); //递归插入
        } else val = x; //d==0
    }
    void erase(int d) {
        if (d) {
            if (sons[d & 1] == nullptr) return;
            sons[d & 1]->erase(d >> 1);
        } else val = -1;
    }
    int query(int d) {
        if (d) {
            if (sons[d & 1] == nullptr) return -1;
            return sons[d & 1]->query(d >> 1);
        } else return val;
    }
};
class MyHashMap {
private:
    Trie01Node* root;
public:
    /** Initialize your data structure here. */ 
    MyHashMap() { root = new Trie01Node(-1); }
    
    /** value will always be non-negative. */
    void put(int key, int value) { root->insert(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 root->query(key); }
    
    /** Removes the mapping of the specified value key if this map contains a mapping for the key */
    void remove(int key) { root->erase(key); }
}; 

运行效率如下:

执行用时:140 ms, 在所有 C++ 提交中击败了70.70% 的用户
内存消耗:57.2 MB, 在所有 C++ 提交中击败了38.59% 的用户

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

memcpy0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值