Go语言LeetCode题解706设计哈希映射

2022-12-29 08:56:51
目录
题目描述思路分析AC 代码

题目描述

706.>

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

实现 MyHashMap 类:

    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 。  

    示例:

    输入:
    ["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
    [[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
    输出:
    [null, null, null, 1, -1, null, 1, null, -1]
    解释:
    MyHashMap myHashMap = new MyHashMap();
    myHashMap.put(1, 1); // myHashMap 现在为 [[1,1]]
    myHashMap.put(2, 2); // myHashMap 现在为 [[1,1], [2,2]]
    myHashMap.get(1);    // 返回 1 ,myHashMap 现在为 [[1,1], [2,2]]
    myHashMap.get(3);    // 返回 -1(未找到),myHashMap 现在为 [[1,1], [2,2]]
    myHashMap.put(2, 1); // myHashMap 现在为 [[1,1], [2,1]](更新已有的值)
    myHashMap.get(2);    // 返回 1 ,myHashMap 现在为 [[1,1], [2,1]]
    myHashMap.remove(2); // 删除键为 2 的数据,myHashMap 现在为 [[1,1]]
    myHashMap.get(2);    // 返回 -1(未找到),myHashMap 现在为 [[1,1]]
    

    提示:

      0 <= key, value <= 10^6最多调用 104 次 put、get 和 remove 方法

      思路分析

        用切片+链表来作为散列表数据结构,利用切片的下标快速定位槽,用链表作为槽存放真正的数据最开始槽用的数组结构,但发现数组增删元素时存在内存的动态申请和释放,每次执行耗时比较长,后来改用链表耗时有明显下降;通过key的哈希值对链表长度取模来定位到槽位;每个槽位有数据时才链接到链表头,没有数据时空间自然释放;具体增、删元素的细节逻辑参考代码注释。

        AC>
        type MyHashMap struct {
            data []*Node    // 切片+链表构成散列表
            cap int         // 容量
        }
        // 链表节点
        type Node struct {
            k int
            v int
            next *Node
        }
        /** Initialize your data structure here. */
        func Constructor() MyHashMap {
            return MyHashMap{
                data: make([]*Node, 1000),
                cap: 1000,
            }
        }
        /** value will always be non-negative. */
        func (this *MyHashMap) Put(key int, value int)  {
            index := key % this.cap
            // put时有三种Case:
            // 1.槽不存在,直接新创建一个槽添加该元素
            // 2.槽存在且已经包含该元素,修改元素值,由于元素是值拷贝方式,需要重新赋值回去才能生效
            // 3.槽存在但不包含该元素,追加元素
            node := this.data[index]
            if node == nil {
               this.data[index] = &Node{key, value, nil}
               return
            }
            for {
                if node.k == key {
                    node.v = value
                    return
                }
                if node.next == nil {
                    node.next = &Node{key, value, nil}
                    return
                }
                node = node.next
            }
        }
        /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
        func (this *MyHashMap) Get(key int) int {
            node := this.data[key % this.cap]
            for node != nil {
                if node.k == key {
                    return node.v
                }
                node = node.next
            }
            return -1
        }
        /** Removes the mapping of the specified value key if this map contains a mapping for the key */
        func (this *MyHashMap) Remove(key int)  {
            node := this.data[key % this.cap]
            var pre *Node
            for node != nil {
                if node.k == key {
                    break
                }
                pre, node = node, node.next
            }
            // 未找到待删除元素
            if node == nil {
                return
            }
            // 如果找到元素,有两种情况:
            // 1.待删除元素是头节点,需要将新的头节点更新到散列表中
            // 2.待删除元素是普通节点,则直接让前驱节点指向下一个节点
            if pre == nil {
                this.data[key % this.cap] = node.next
            }else {
                pre.next = node.next
            }
        }
        /**
         * Your MyHashMap object will be instantiated and called as such:
         * obj := Constructor();
         * obj.Put(key,value);
         * param_2 := obj.Get(key);
         * obj.Remove(key);
         */

        以上就是Go语言LeetCode题解706设计哈希映射的详细内容,更多关于Go语言设计哈希映射的资料请关注易采站长站其它相关文章!