链接: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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。