LeetCode·49.字母异位词分组·hash

链接:https://leetcode.cn/problems/group-anagrams/solution/hash-by-xun-ge-v-k2lw/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 

题目

 

示例

 

思路

解题思路
对于hash算法不是特别了解的可以看哈希算法详解,讲的非常全面

对于本题,因为字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。所以我们可以将字符串数组中的所有字符串重新排序,然后加入hash中,如果在hash中存在则存储在同一组,如果不存在则单独分组
注意hash键应该为字符串

代码

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
struct hashTable {
    char* key;
    int index;
    int cnt;
    UT_hash_handle hh;
};
int cmp (const void* a, const void* b) {
    return *(char*)a > *(char*)b ? 1 : 0;
}
char *** groupAnagrams(char ** strs, int strsSize, int* returnSize, int** returnColumnSizes){
    if (strsSize == 0) return NULL;
    char ***res = (char ***)malloc(sizeof(char **) * strsSize);
    *returnColumnSizes = (int *)malloc(sizeof(int) * strsSize);
    *returnSize = 0;
    struct hashTable *set = NULL;
    for(int i=0; i<strsSize; i++){
        int len = strlen(strs[i]);
        char strtmp[len+1];
        memset(strtmp, 0, sizeof(char) * (len+1));
        memcpy(strtmp, strs[i], sizeof(char) * len);
        // qsort tmp
        qsort(strtmp, len, sizeof(char), cmp);
        struct hashTable *tmp;
        HASH_FIND_STR(set, strtmp, tmp);
        if(tmp == NULL) {
            // HASH表中没有
            tmp= (struct hashTable *)malloc(sizeof(struct hashTable));
            tmp->key = (char*)calloc(len + 1, sizeof(char));
            memset(tmp->key, 0, sizeof(char) * (len + 1));
            memcpy(tmp->key, strtmp, sizeof(char) * len);
            tmp->index = *returnSize;
            tmp->cnt = 1;
            HASH_ADD_STR(set, key, tmp);
            res[*returnSize] = (char **)malloc(sizeof(char*) * strsSize);
            res[*returnSize][tmp->cnt-1] = (char *)malloc(sizeof(char) * (len + 1));
            memset(res[*returnSize][tmp->cnt-1], 0, sizeof(char) * (len + 1));
            memcpy(res[*returnSize][tmp->cnt-1], strs[i], sizeof(char) * len);
            (*returnColumnSizes)[*returnSize] = tmp->cnt;
            (*returnSize)++;
        }
        else {
            // HASH表中有记录
            res[tmp->index][tmp->cnt] = (char *)malloc(sizeof(char) * (len + 1));
            memset(res[tmp->index][tmp->cnt], 0, sizeof(char) * (len + 1));
            memcpy(res[tmp->index][tmp->cnt], strs[i], sizeof(char) * len);
            tmp->cnt += 1;
            (*returnColumnSizes)[tmp->index] = tmp->cnt;

        }
    }

    return res;

}
 


作者:xun-ge-v
链接:https://leetcode.cn/problems/group-anagrams/solution/hash-by-xun-ge-v-k2lw/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值