难度中等1012收藏分享切换为英文接收动态反馈
    给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
    字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。

    示例 1:
    输入: strs = [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”] 输出: [[“bat”],[“nat”,”tan”],[“ate”,”eat”,”tea”]]
    示例 2:
    输入: strs = [“”] 输出: [[“”]]
    示例 3:
    输入: strs = [“a”] 输出: [[“a”]]

    提示:

    • 1 <= strs.length <= 104
    • 0 <= strs[i].length <= 100
    • strs[i] 仅包含小写字母
    1. #define HANODELEN 700
    2. typedef struct node_t{
    3. int index;
    4. int key;
    5. int cnt[27];
    6. int rawSeq;
    7. int colSeq;
    8. struct node_t* next;
    9. }Node;
    10. typedef struct hanode_t{
    11. int index;
    12. int cnt;
    13. Node* next;
    14. }HaNode;
    15. int hash_insert(HaNode* hanodes, Node* node){
    16. int ret = 0;
    17. int index = node->key%HANODELEN;
    18. printf("hash_insert index %d\r\n", index);
    19. HaNode* hanode = hanodes+index;
    20. Node* curnode = hanode->next;
    21. if(curnode==NULL)
    22. {
    23. node->rawSeq = hanode->index;
    24. node->colSeq = hanode->cnt;
    25. hanode->next = node;
    26. hanode->cnt += 1;
    27. return 0;
    28. }
    29. else
    30. {
    31. while(curnode)
    32. {
    33. if(curnode->key==node->key && !memcmp(curnode->cnt,node->cnt, sizeof(node->cnt)))
    34. {
    35. node->rawSeq = hanode->index;
    36. node->colSeq = hanode->cnt;
    37. node->next=curnode->next;
    38. curnode->next=node;
    39. hanode->cnt += 1;
    40. return 0;
    41. }
    42. curnode=curnode->next;
    43. }
    44. node->rawSeq = hanode->index;
    45. node->colSeq = 0;
    46. node->next = hanode->next;
    47. hanode->next = node;
    48. hanode->cnt += 1;
    49. }
    50. printf("hanode->cnt %d\r\n", hanode->cnt);
    51. return 0;
    52. }
    53. /**
    54. * Return an array of arrays of size *returnSize.
    55. * The sizes of the arrays are returned as *returnColumnSizes array.
    56. * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
    57. */
    58. char *** groupAnagrams(char ** strs, int strsSize, int* returnSize, int** returnColumnSizes){
    59. int i,j;
    60. *returnSize=0;
    61. HaNode* hanodes = (HaNode*)malloc(HANODELEN*sizeof(HaNode));
    62. char*** output = NULL;
    63. Node* nodes = malloc(strsSize*sizeof(Node));
    64. memset(nodes, 0, strsSize*sizeof(Node));
    65. memset(hanodes, 0, HANODELEN*sizeof(HaNode));
    66. for(i=0; i<HANODELEN; i++)
    67. {
    68. hanodes[i].index=i;
    69. }
    70. for(i=0; i<strsSize; i++)
    71. {
    72. j=0;
    73. nodes[i].index=i;
    74. while(strs[i][j]!='\0')
    75. {
    76. nodes[i].key+=strs[i][j]-'a'+1;
    77. nodes[i].cnt[strs[i][j]-'a'+1]+=1;
    78. j++;
    79. }
    80. }
    81. for(i=0; i<strsSize; i++)
    82. {
    83. hash_insert(hanodes, nodes+i);
    84. printf("nodes[i].colSeq %d\r\n", nodes[i].colSeq);
    85. if(nodes[i].colSeq==0)
    86. {
    87. *returnSize+=1;
    88. }
    89. }
    90. int* outputColumnSizes = malloc((*returnSize)*sizeof(int*));
    91. output = malloc(strsSize*sizeof(char**));
    92. printf("output raw len %d\r\n", *returnSize);
    93. #if 1
    94. int k=0, l=0;
    95. for(i=0; i<HANODELEN; i++)
    96. {
    97. Node* lastNode = NULL;
    98. Node* curNode = hanodes[i].next;
    99. if(curNode)
    100. {
    101. l=0;
    102. printf("hanodes[i].cnt %d\r\n", hanodes[i].cnt);
    103. output[k]=malloc(hanodes[i].cnt*sizeof(char*));
    104. outputColumnSizes[k]=0;
    105. }
    106. else
    107. {
    108. continue;
    109. }
    110. while(curNode)
    111. {
    112. if(lastNode!=NULL && memcmp(lastNode->cnt, curNode->cnt, sizeof(curNode->cnt)))
    113. {
    114. outputColumnSizes[k]=l;
    115. k++;
    116. l=0;
    117. output[k]=malloc(hanodes[i].cnt*sizeof(char*));
    118. }
    119. output[k][l]=strs[curNode->index];
    120. printf("str %s\r\n", output[k][l]);
    121. lastNode=curNode;
    122. curNode=curNode->next;
    123. l++;
    124. }
    125. outputColumnSizes[k]=l;
    126. printf("k %d cnt %d\r\n", k, outputColumnSizes[k]);
    127. k++;
    128. }
    129. #endif
    130. *returnColumnSizes=outputColumnSizes;
    131. printf("returnSize %d\r\n", *returnSize);
    132. return output;
    133. }