【哈希表】Java

哈希表

给每份数据分配一个编号,放入表格(数组)建立编号与表格索引的关系,将来就可以通过编号快速查找数据
1.理想情况编号当唯一,数组能容纳所有数据
2.现实是不能说为了容纳所有数据造一个超大数组,编号也有可能重复
解决
1.有限长度的数组,以【拉链】方式存储数据
2.允许编号适当重复,通过数据自身来进行区分

package HasTable;

public class HashTable {
    // 节点类
    static class Entry {
        int hash; // 哈希码
        Object key; // 键
        Object value; // 值
        Entry next;

        // 构造函数
        public Entry(int hash, Object key, Object value, Entry next) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = next;
        }
    }

    Entry[] table = new Entry[16]; // 哈希表数组
    int size = 0; // 元素个数

    // 根据hash码获取value
    public Object get(int hash, Object key) {
        /**
         * 求模运算替换为位运算工
         * 前提:数组长度是2的n次方
         * -hash % 数组长度 等价于 hash &(数组长度-1)
         */
        int index = hash & (table.length - 1); // 计算数组下标
        Entry entry = table[index]; // 获取对应的链表
        while (entry != null) {
            if (entry.key.equals(key)) {
                return entry.value; // 找到对应的节点,返回值
            }
            entry = entry.next; // 继续查找下一个节点
        }
        return null; // 没有找到对应的节点,返回null
    }

    // 向哈希表存入新key value,如果key重复则更新value
    public void put(int hash, Object key, Object value) {
        int index = hash & (table.length - 1); // 计算数组下标
        Entry entry = table[index]; // 获取对应的链表
        if (entry == null) {
            // 链表为空,直接插入新节点
            table[index] = new Entry(hash, key, value, null);
            size++; // 元素个数加1
        } else {
            Entry prev = null;
            while (entry != null) {
                if (entry.hash == hash && entry.key.equals(key)) {
                    // 找到重复的节点,更新值
                    if (prev == null) {
                        table[index] = new Entry(hash, key, value, entry.next);
                    } else {
                        prev.next = new Entry(hash, key, value, entry.next);
                    }
                    return;
                }
                prev = entry; // 保存前一个节点
                entry = entry.next; // 继续查找下一个节点
            }
            // 遍历完链表,没有找到重复的节点,插入新节点
            prev.next = new Entry(hash, key, value, null);
            size++; // 元素个数加1
        }
    }

    // 根据hash码返回,返回删除的value
    public Object remove(int hash) {
        int index = hash & (table.length - 1); // 计算数组下标
        Entry entry = table[index]; // 获取对应的链表
        Entry prev = null;
        while (entry != null) {
            if (entry.hash == hash) {
                // 找到要删除的节点
                if (prev == null) {
                    table[index] = entry.next; // 更新链表头节点
                } else {
                    prev.next = entry.next; // 更新前一个节点的next指针
                }
                size--; // 元素个数减1
                return entry.value; // 返回删除节点的值
            }
            prev = entry; // 保存前一个节点
            entry = entry.next; // 继续查找下一个节点
        }
        return null; // 没有找到要删除的节点,返回null
    }
}

哈希表相关算法

https://leetcode.cn/problems/two-sum/submissions/513068308/

    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer,Integer>map = new HashMap<>();
        for (int i=0;i<nums.length;i++){
            int x=nums[i];
            int y = target-x;
            if (map.containsKey(y)){
                 return new int []{i,map.get(y)};
            }else {
                map.put(x,i);
            }
        }
        return null;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值