2024.4.14力扣每日一题——设计哈希集合

2024.4.14

题目来源

力扣每日一题;题序:705

我的题解

方法一 链表数组

由于给定限制次数为10000,所以构造一个长度为10001的链表数组。对于add操作先看数组对应的位置是否为null或者为空,若是则直接加入,否则遍历整个链表看是否有与加入的值相同的元素。对于remove操作,先看数组对应的位置是否为null或者为空,若是则直接退出,否则遍历整个链表看是否有与加入的值相同的元素,若相同则删除对应的链表节点。对于contains操作,先看数组对应的位置是否为null或者为空,若是则直接返回false,否则遍历整个链表看是否有与加入的值相同的元素,若有直接返回true,否则返回false。

对于哈希函数的设计:取key对应的哈希值mod 10000

哈希冲突的解决:使用链地址法解决

java 复制代码
class MyHashSet {
    class LinkedList{
        int val;
        LinkedList next;
        public LinkedList(){}
        public LinkedList(int v){
            val=v;
        }
        public int size(){
            LinkedList root=this;
            int sz=0;
            while(root!=null){
                sz++;
                root=root.next;
            }
            return sz;
        }
    }

    private LinkedList[] keys;
    int n=10001;

    public MyHashSet() {
        keys=new LinkedList[n];
        // Arrays.fill(keys,new LinkedList());
    }

    public void add(int key) {
        int index=myHash(key);
        // 节点为空
        if(keys[index]==null){
            keys[index]=new LinkedList(key);
            // 还未有元素
        }else if(keys[index].size()==0){
            keys[index].val=key;
            //已经有元素
        }else{
            LinkedList root=keys[index];
            if (root.val==key)
                return ;
            while(root.next!=null&&root.next.val!=key){
                root=root.next;
            }
            if(root.next==null)
                root.next=new LinkedList(key);
        }
    }

    public void remove(int key) {
        int index=myHash(key);
        // 节点为空 || 还未有元素
        if(keys[index]==null||keys[index].size()==0)
            return ;
            //已经有元素
        else{
            LinkedList root=keys[index];
            if(root.val==key){
                keys[index]=root.next;
            }else{
                while(root.next!=null&&root.next.val!=key){
                    root=root.next;
                }
                if(root.next!=null)
                    root.next=root.next.next;
            }
        }
    }

    public boolean contains(int key) {
        int index=myHash(key);
        // 节点为空 || 还未有元素
        if(keys[index]==null||keys[index].size()==0)
            return false;
            //已经有元素
        else{
            LinkedList root=keys[index];
            while(root!=null){
                if(root.val==key)
                    return true;
                root=root.next;
            }
            return false;
        }

    }

    public int myHash(int key){
        int iHash=Integer.hashCode(key);
        return iHash%(n-1);
    }

    @Override
    public String toString() {
        return Arrays.toString(keys);
    }
}

有任何问题,欢迎评论区交流,欢迎评论区提供其它解题思路(代码),也可以点个赞支持一下作者哈😄~

相关推荐
heartbeat..几秒前
Web 状态管理核心技术详解 + JWT 双 Token (Access/Refresh Token) 自动登录
java·网络·jwt·token
Seven972 分钟前
剑指offer-57、二叉树的下一个节点
java
hetao17338373 分钟前
2025-12-30 hetao1733837 的刷题笔记
c++·笔记·算法
DYS_房东的猫3 分钟前
Spring Boot集成华为云OBS实现文件上传与预览功能(含安全下载)
java·spring boot
小袁顶风作案6 分钟前
leetcode力扣——27.移除元素、26.删除有序数组的重复项、80.删除有序数组中的重复项 II
数据结构·算法·leetcode
曹轲恒7 分钟前
jvm 局部变量表slot复用问题
java·开发语言·jvm
小王师傅669 分钟前
【轻松入门SpringBoot】actuator健康检查(中)-group,livenessState,readinessState
java·spring boot·后端
青w韵10 分钟前
最新SpringAI-1.1.2接入openai兼容模型
java·学习·ai·springai
222you10 分钟前
SpringMVC的单文件上传
java·开发语言
培培说证12 分钟前
2026大专跨境电商专业,想好就业考哪些证书比较好?
java