2024.4.15力扣每日一题——设计哈希映射

题目来源

力扣每日一题;题序:706

我的题解

方法一 链表+自定义哈希函数

使用链表存储每个<key,value>。由于题目有限制put的次数不超过10000次,因此对于哈希函数的设计为:hashCode(key)%10000

class MyHashMap {
    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[] map;
    int n=10001;
    public MyHashMap() {
        map=new LinkedList[n];

    }
    
    public void put(int key, int value) {
        int index=myHash(key);
        //对应位置还未初始化
        if(map[index]==null){
            map[index]=new LinkedList(new int[]{key,value});
        //对应位置的长度为0
        }else if(map[index].size()==0){
            map[index]=new LinkedList(new int[]{key,value});
        }else{
            LinkedList root=map[index];
            //链首就是key
            if(root.val[0]==key){
                root.val[1]=value;
            }else{
                while(root.next!=null&&root.next.val[0]!=key){
                    root=root.next;
                }
                //在链中存在key
                if(root.next!=null){
                    root.next.val[1]=value;
                //链中不存在key
                }else{
                    root.next=new LinkedList(new int[]{key,value});
                }
            }
        }
    }
    
    public int get(int key) {
        int index=myHash(key);
        //对应位置还未初始化
        if(map[index]==null||map[index].size()==0){
            return -1;
        }else{
            LinkedList root=map[index];
            //链首就是key
            if(root.val[0]==key){
                return root.val[1];
            }else{
                while(root.next!=null&&root.next.val[0]!=key){
                    root=root.next;
                }
                //在链中存在key
                if(root.next!=null){
                    return root.next.val[1];
                //链中不存在key
                }else{
                   return -1;
                }
            }
        }
    }
    
    public void remove(int key) {
        int index=myHash(key);
        //对应位置还未初始化||对应位置的长度为0
        if(map[index]==null||map[index].size()==0){
            return ;
        }else{
            LinkedList root=map[index];
            //链首就是key
            if(root.val[0]==key){
                map[index]=root.next;
            }else{
                while(root.next!=null&&root.next.val[0]!=key){
                    root=root.next;
                }
                //在链中存在key
                if(root.next!=null){
                    root.next=root.next.next;
                //链中不存在key
                }
            }
        }
    }
    private int myHash(int key){
        int iHash=Integer.hashCode(key);
        return iHash%(n-1);
    }
}

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜菜的小彭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值