其他分享
首页 > 其他分享> > LeetCode 每日一题706. 设计哈希映射

LeetCode 每日一题706. 设计哈希映射

作者:互联网

706. 设计哈希映射

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

实现 MyHashMap 类:

示例:

输入:
["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]]

提示:

方法一:链地址法

解题思路

昨天 [705. 设计哈希集合] 直接强撸的。今天用链地址法,从 Node 开始撸。

参考代码

class MyHashMap {

    private final Node[] table;

    private final int CAPACITY = 1 << 10;

    /** Initialize your data structure here. */
    public MyHashMap() {
        table = new Node[CAPACITY];
    }

    /** value will always be non-negative. */
    public void put(int key, int value) {
        int index = indexFor(key);
        Node node = table[index];
        if (node == null) {
            node = new Node(key, value, null);
        } else {
            Node head = node;
            while (node != null) {
                if (node.key == key) {
                    node.value = value;
                    return;
                }
                node = node.next;
            }
            node = new Node(key, value, head);
        }
        table[index] = node;
    }

    /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
    public int get(int key) {
        int index = indexFor(key);
        Node node = table[index];
        while (node != null) {
            if (node.key == key) {
                return node.value;
            }
            node = node.next;
        }
        return -1;
    }

    /** Removes the mapping of the specified value key if this map contains a mapping for the key */
    public void remove(int key) {
        int index = indexFor(key);
        Node node = table[index];
        if (node == null) {
            return;
        }
        if (node.key == key) {
            table[index] = node.next;
            return;
        }
        Node pre = node;
        node = node.next;
        while (node != null) {
            if (node.key == key) {
                pre.next = node.next;
                return;
            }
            pre = node;
            node = node.next;
        }
    }

    private int indexFor(int key) {
        return key & CAPACITY - 1;
    }

    class Node {
        int key;
        int value;
        Node next;
        Node(int key, int value, Node next) {
            this.key = key;
            this.value = value;
            this.next = next;
        }
    }
}

执行结果

在这里插入图片描述

标签:node,Node,哈希,int,706,value,myHashMap,key,LeetCode
来源: https://blog.csdn.net/qq_27007509/article/details/114802961