首页 > 其他分享 >leetcode 127 -- 哈希表

leetcode 127 -- 哈希表

时间:2022-09-18 19:24:03浏览次数:120  
标签:Node set obj 哈希 -- int key leetcode cur

题目描述

217

手写哈希表

class Solution {
public:
    #define DEFAULT_LEN 16
    #define DEFAULT_FACTOR 0.75f
    const float factor = DEFAULT_FACTOR;
    typedef struct node{
        int key;
        int val;
        struct node* next;
    }Node;
    typedef struct {
        int capacity;
        int size;
        Node** set;
    } MyHashMap;


    MyHashMap* init() {
        MyHashMap* ret =  (MyHashMap*)malloc(sizeof(MyHashMap));
        ret->capacity = DEFAULT_LEN;
        ret->size = 0;
        ret->set = (Node**)calloc(DEFAULT_LEN,sizeof(Node*));
        return ret;
    }
    inline unsigned int getIndex(unsigned int key,int capacity){
        return (key^(key>>16))&(capacity-1);
    }
    void insert(MyHashMap* obj,Node* node){
         int index = getIndex(node->key,obj->capacity);
         node->next = obj->set[index];
         obj->set[index] = node;
    }
    void rehash(MyHashMap* obj,Node** tmp){
        int preSize = obj->capacity / 2;
        for(int i=0;i<preSize;i++){
            if(tmp[i]!=NULL){
                Node * pre;
                Node * cur = tmp[i];
                while (cur!=NULL){
                    pre = cur;
                    cur = cur->next;
                    insert(obj,pre);
                }
            }
        }
    }
    void resize(MyHashMap* obj){
        obj->capacity <<= 1;
        Node ** tmp = obj->set;
        obj->set = (Node**)calloc(obj->capacity,sizeof(Node*));
        rehash(obj,tmp);
        free(tmp);
    }
    void Put(MyHashMap* obj, int key,int value) {
        if(obj->size>=factor*obj->capacity)resize(obj);
        int index = getIndex(key,obj->capacity );
        if(obj->set[index]==NULL){
            Node * t = (Node*)malloc(sizeof(Node));
            t->key = key;
            t->val = value;
            t->next = NULL;
            obj->set[index] = t;
            obj->size++;
            return;
        }
        Node* head = obj->set[index];
        Node* t = head;
        while (t!=NULL){
            if(t->key==key){
                t->val = value;
                return;
            }
            t = t->next;
        }
        Node* node = (Node*)malloc(sizeof(Node));
        node->key = key;
        node->val = value;
        node->next = head;
        obj->set[index] = node;
        obj->size++;
    }

    void Remove(MyHashMap* obj, int key) {
        int index = getIndex(key,obj->capacity);
        Node *head = obj->set[index];
        Node *pre;
        Node *cur = head;
        if(cur==NULL)
            return;
        if(cur->key == key){
            pre = cur;
            cur = cur->next;
            free(pre);
            obj->set[index] = cur;
            return;
        }
        while (cur!=NULL){
            pre = cur;
            cur = cur->next;
            if(cur!=NULL&&cur->key == key){
                pre->next = cur->next;
                free(cur);
                return;
            }
        }

    }

    int* Get(MyHashMap* obj, int key) {
        int index = getIndex(key,obj->capacity);
        Node * head = obj->set[index];
        while (head != NULL){
            if(head->key == key)
                return &(head->val);
            head = head->next;
        }
        return NULL;
    }

    void del_node(Node* head){
        Node * pre;
        Node * cur = head;
        while (cur!=NULL){
            pre = cur;
            cur = cur->next;
            free(pre);
        }
    }
    void Free(MyHashMap* obj) {
        for(int i=0;i<obj->capacity;i++){
            del_node(obj->set[i]);
        }
    }

    bool containsDuplicate(vector<int>& nums) {
        MyHashMap* set = init();
        int n = nums.size();
        for (int i = 0; i < n; i++) {
            if(Get(set,nums[i])!=NULL)
                return true;
            Put(set,nums[i],0);
        }
        return false;
    }
};

标签:Node,set,obj,哈希,--,int,key,leetcode,cur
From: https://www.cnblogs.com/ALaterStart/p/16705514.html

相关文章

  • 解决文件夹里右键“创建文本文件”选项消失的问题
    新建一个文本输入以下内容WindowsRegistryEditorVersion5.00[HKEY_CLASSES_ROOT\.txt]@="txtfile""ContentType"="text/plain"[HKEY_CLASSES_ROOT\.txt\Shel......
  • Typescript类型体操 - ReplaceKeys
    题目中文实现一个ReplaceKeys类型,这个类型可以替换联合类型中指定属性的类型,如果联合类型中的某个类型没有这个属性,那就跳过;ReplaceKeys接受3个泛型参数.例如......
  • 自我介绍和规划
    自我介绍回首匆匆三年,目前我还是在大三。本人思想积极上进,性格开朗,生活作风严谨,责任心强,办事沉稳、执着,能吃苦耐劳,适应性强,具有良好的心理素质。现状经验、计划接下来我......
  • 斐波那契数列
    斐波那契数列分析:斐波那契数列是后一个数等于前两个数之和,所以开一个变量存每个算出的数所在的位置,然后输出指定的第a个数的值。在这里开一个maxx,得出最大的a是谁,就把斐波......
  • linux mysql数据 解决ERROR 1045 (28000): Access denied for user 'root'@'localhost
    在linux系统是输入命令: mysql-uroot-p输入密码后 提示 ERROR1045(28000):Accessdeniedforuser'root'@'localhost'(usingpassword:YES):说明输入的密码是......
  • CSP2021-s题解
    T1廊桥分配题目链接P7913[CSP-S2021]廊桥分配\(Lemma:\)对于已经存在的廊桥集合,加入新的廊桥不会影响之前分配好的航班。证明:由于题目所给限制,若有空廊桥,则航......
  • 内部类
    内部类在方法体或代码块局部内部类1、可以访问外部类的所用成员,包括私有的2、不能添加访问修饰符,因为局部内部类跟局部变量一样,可以加final关键字。3、作用域:仅仅在定......
  • 你可得知道物理地址与IP地址
    来看看计算机网络中这些常见的概念你有没有理解~物理地址表示方式物理地址即mac地址,每个网卡都有6字节的唯一标识,前三个字节表示厂商,后三个字节由厂商随机分配。如何......
  • css简单动画 @-webkit-keyframes、-webkit-transform、webkit-animation的使用
    浏览器前缀IE10和Firefox(>=16)支持没有前缀的animation,firefox(<16)使用-moz-前缀,因为现在firefox的版本也都不低,所以firefox都直接使用没有前缀的animation。而chrome,safa......
  • python实训2
    test3-1print("今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二,问几何?\n")number=int(input("请输入您认为符合条件的数:"))ifnumber%3==2andnumber%5==3andnumb......