难度中等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] 仅包含小写字母
#define HANODELEN 700typedef struct node_t{int index;int key;int cnt[27];int rawSeq;int colSeq;struct node_t* next;}Node;typedef struct hanode_t{int index;int cnt;Node* next;}HaNode;int hash_insert(HaNode* hanodes, Node* node){int ret = 0;int index = node->key%HANODELEN;printf("hash_insert index %d\r\n", index);HaNode* hanode = hanodes+index;Node* curnode = hanode->next;if(curnode==NULL){node->rawSeq = hanode->index;node->colSeq = hanode->cnt;hanode->next = node;hanode->cnt += 1;return 0;}else{while(curnode){if(curnode->key==node->key && !memcmp(curnode->cnt,node->cnt, sizeof(node->cnt))){node->rawSeq = hanode->index;node->colSeq = hanode->cnt;node->next=curnode->next;curnode->next=node;hanode->cnt += 1;return 0;}curnode=curnode->next;}node->rawSeq = hanode->index;node->colSeq = 0;node->next = hanode->next;hanode->next = node;hanode->cnt += 1;}printf("hanode->cnt %d\r\n", hanode->cnt);return 0;}/*** 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().*/char *** groupAnagrams(char ** strs, int strsSize, int* returnSize, int** returnColumnSizes){int i,j;*returnSize=0;HaNode* hanodes = (HaNode*)malloc(HANODELEN*sizeof(HaNode));char*** output = NULL;Node* nodes = malloc(strsSize*sizeof(Node));memset(nodes, 0, strsSize*sizeof(Node));memset(hanodes, 0, HANODELEN*sizeof(HaNode));for(i=0; i<HANODELEN; i++){hanodes[i].index=i;}for(i=0; i<strsSize; i++){j=0;nodes[i].index=i;while(strs[i][j]!='\0'){nodes[i].key+=strs[i][j]-'a'+1;nodes[i].cnt[strs[i][j]-'a'+1]+=1;j++;}}for(i=0; i<strsSize; i++){hash_insert(hanodes, nodes+i);printf("nodes[i].colSeq %d\r\n", nodes[i].colSeq);if(nodes[i].colSeq==0){*returnSize+=1;}}int* outputColumnSizes = malloc((*returnSize)*sizeof(int*));output = malloc(strsSize*sizeof(char**));printf("output raw len %d\r\n", *returnSize);#if 1int k=0, l=0;for(i=0; i<HANODELEN; i++){Node* lastNode = NULL;Node* curNode = hanodes[i].next;if(curNode){l=0;printf("hanodes[i].cnt %d\r\n", hanodes[i].cnt);output[k]=malloc(hanodes[i].cnt*sizeof(char*));outputColumnSizes[k]=0;}else{continue;}while(curNode){if(lastNode!=NULL && memcmp(lastNode->cnt, curNode->cnt, sizeof(curNode->cnt))){outputColumnSizes[k]=l;k++;l=0;output[k]=malloc(hanodes[i].cnt*sizeof(char*));}output[k][l]=strs[curNode->index];printf("str %s\r\n", output[k][l]);lastNode=curNode;curNode=curNode->next;l++;}outputColumnSizes[k]=l;printf("k %d cnt %d\r\n", k, outputColumnSizes[k]);k++;}#endif*returnColumnSizes=outputColumnSizes;printf("returnSize %d\r\n", *returnSize);return output;}
