题目:
题解:
/*1.将字符串原串与副本进行绑定成一个节点2.对字符串副本进行按ascii码表进行从小到大排序3.按照字符串进行比较排序4.合并
*/
typedef struct Node{char*s;char*s_vice;int len;
}Node;void sortShellChar(char*s,int len){for(int distance=len/2;distance!=0;distance/=2){for(int i=distance;i<len;i++){char c=s[i];int pre=i-distance;while(pre>=0&&s[pre]>c){s[pre+distance]=s[pre];pre-=distance;}if(pre+distance!=i){s[pre+distance]=c;}}}
}void sortShellString(Node*arr,int size){for(int distance=size/2;distance!=0;distance/=2){for(int i=distance;i<size;i++){int pre=i-distance;Node node=arr[i];while(pre>=0&&strcmp(arr[pre].s_vice,node.s_vice)<0){arr[pre+distance]=arr[pre];pre-=distance;}if(pre+distance!=i){arr[pre+distance]=node;}}}
}char*** groupAnagrams(char** strs, int strsSize, int* returnSize, int** returnColumnSizes) {Node*strsNode=malloc(sizeof(Node)*strsSize);int strsNodeSize=strsSize;for(int i=0;i<strsSize;i++){strsNode[i].len=strlen(strs[i]);strsNode[i].s=strs[i];strsNode[i].s_vice=malloc(sizeof(char)*( strsNode[i].len+1));strcpy(strsNode[i].s_vice, strsNode[i].s);sortShellChar(strsNode[i].s_vice, strsNode[i].len);}sortShellString(strsNode, strsNodeSize);char***stack=malloc(sizeof(char**)*9869);int top=-1;int start=0;*returnColumnSizes=calloc(9869,sizeof(int));while(start<strsNodeSize){int end=start+1;while(end<strsNodeSize&&strcmp(strsNode[start].s_vice,strsNode[end].s_vice)==0){end++;}char**mergeWords=malloc(sizeof(char*)*(end-start));for(int i=0;i<end-start;i++){mergeWords[i]=strsNode[start+i].s;}stack[++top]=mergeWords;(*returnColumnSizes)[top]=end-start;start=end;}*returnSize=top+1;return stack;
}