LeetCode 49 字母异位词分组 C语言 哈希表实现

本文介绍了使用C语言通过哈希表解决LeetCode第49题——字母异位词分组的问题。文章提供了详细解题思路和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码来源https://leetcode-cn.com/problems/group-anagrams/solution/cyu-yan-sui-ran-xie-qi-lai-hen-lei-dan-shi-dui-ha-/

 

#define STR_SIZE 100

typedef struct Node{
    char str[STR_SIZE]; // key为字符串
    int row;            // value为结果所在的行
    struct Node * next;
} HashNode;


int hash(char * str, int size)
{
    long h = 0;
    for(int i = 0; i < strlen(str); i++){
        h = (h * 26 % size + str[i] - 'a') % size; // 字符串的hashcode, 权为26是因为小写字母,不限制时为128,这样能够让结点尽可能分布均匀,减少地址冲突
        // 取模是为了防止int型溢出
    }
    return h % size;
}

bool contain(HashNode * hashtable, char * str, int size)
{
    HashNode *head = &hashtable[hash(str, size)];
    HashNode *tail = head->next;
    while(tail){
        if(strcmp(tail->str, str) == 0) retur
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值