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

文章目录


706. 设计哈希映射

思路:链地址法
代码:
java 复制代码
public class MyHashMap {
    static class Node {
        int key;
        int val;
        Node next;

        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }
    Node[] nodes = new Node[10009];

    public void put(int key, int value) {
        int index = getIndex(key);
        Node cur = nodes[index];
        Node tmp = cur;
        if (cur != null) {
            Node prev = null;
            while (tmp != null) {
                if (tmp.key == key) {
                    tmp.val = value;
                    return;
                }
                prev = tmp;
                tmp = tmp.next;
            }
            tmp = prev;//找到末尾
        }
        Node node = new Node(key, value);
        //头插法
        if (tmp != null) {
            tmp.next = node;
        } else {
            nodes[index] = node;
        }
    }
    public void remove(int key) {
        int index = getIndex(key);
        Node cur = nodes[index];
        if (cur != null) {
            Node prev = null;
            while (cur != null) {
                if (cur.key == key) {
                    if (prev != null) {
                        prev.next = cur.next;
                    } else {
                        nodes[index] = cur.next;
                    }
                    return;
                }
                prev = cur;
                cur = cur.next;
            }
        }
    }

    public int get(int key) {
        int hash = getIndex(key);
        Node cur = nodes[hash];
        if (cur!=null){
            while (cur!=null){
                if (cur.key == key){
                    return cur.val;
                }
                cur = cur.next;
            }
        }
        return -1;
    }
    public int getIndex(int key){
        int hash = Integer.hashCode(key);
        //数组长度只有 10009,对应的十进制的 10011100011001
        //返回的哈希值是32位的,右移16位的操作是为了将32位的哈希码中高16位的信息与低16位的信息进行混合
        //将高位的信息混合到低位中,增加了哈希值的随机性, 均匀分布
        hash ^= (hash >>> 16);
        return hash % nodes.length;
    }
}
思路:静态数组
代码:
java 复制代码
class MyHashMap {
    private int[]data = new int[1000001];

    public MyHashMap() {
        Arrays.fill(data,-1);

    }
    
    public void put(int key, int value) {
        data[key] = value; 

    }
    
    public int get(int key) {
        return data[key];

    }
    
    public void remove(int key) {
        data[key] = -1;

    }
}
思路:开放地址法
代码:
java 复制代码
public class MyHashMap2 {
    static class Node {
        int key;
        int val;
        boolean isDeleted;
        Node next;

        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }

    Node[] nodes = new Node[10009];
    int OFFSET = 1;

    public void put(int key, int value) {
        int index = getIndex(key);
        Node cur = nodes[index];
        if (cur != null) {
            cur.val = value;
            cur.isDeleted = false;
        }
        Node node = new Node(key, value);
        nodes[index] = node;
    }

    public void remove(int key) {
        Node node = nodes[getIndex(key)];
        if (node!=null){
            node.isDeleted = true;
        }


    }

    public int get(int key) {
        if (nodes[getIndex(key)]==null){
            return -1;
        }
        return nodes[getIndex(key)].isDeleted ? -1 : nodes[getIndex(key)].val;

    }

    private int getIndex(int key) {
        int hash = Integer.hashCode(key);
        hash ^= (hash >>> 16);
        int index = hash % nodes.length;
        while (nodes[index] != null && nodes[index].key != key) {
            hash += OFFSET;
            //该位置存在并且不等于key,哈希值进行偏移
            //重新计算index;
            index = hash % nodes.length;
        }
        return index;
    }

点击移步博客主页,欢迎光临~

相关推荐
passer__jw7671 小时前
【LeetCode】【算法】3. 无重复字符的最长子串
算法·leetcode
passer__jw7671 小时前
【LeetCode】【算法】21. 合并两个有序链表
算法·leetcode·链表
__AtYou__3 小时前
Golang | Leetcode Golang题解之第557题反转字符串中的单词III
leetcode·golang·题解
2401_858286113 小时前
L7.【LeetCode笔记】相交链表
笔记·leetcode·链表
_OLi_5 小时前
力扣 LeetCode 704. 二分查找(Day1:数组)
算法·leetcode·职场和发展
passer__jw7675 小时前
【LeetCode】【算法】11. 盛最多水的容器
算法·leetcode
Wils0nEdwards6 小时前
Leetcode 罗马数字转整数
算法·leetcode·职场和发展
来知晓7 小时前
Python世界:力扣题633,平方数之和,中等
开发语言·python·leetcode
白鹭float.9 小时前
【算法速刷(9/100)】LeetCode —— 42.接雨水
算法·leetcode
Sunyanhui110 小时前
力扣 x的平方根-69
java·算法·leetcode