版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pyl88429/article/details/85070805
重点columnSizes 返回值!!!!!
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *columnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
char*** groupAnagrams(char** strs, int strsSize, int** columnSizes, int* returnSize) {
int comp(const void *a , const void *b ){
return *(int *)a - *(int *)b; //升序排
}
int can[10000][100];
int len[10000];
for(int i=0;i<strsSize;i++)
{
//printf("%s\n",strs[i]);
len[i]=strlen(strs[i]);
for(int j=0;j<strlen(strs[i]);j++)
{
can[i][j]=strs[i][j]-'a';
}
qsort(can[i],len[i],sizeof(int),comp);
}
char *** re = (char***)malloc(sizeof(char**) * 10000);
int red[10000][100];
int rel[10000];
int reptr[10000]={0};
int zu=0;
int sig=0;
for(int u=0;u<strsSize;u++)
{ sig=0;
int ok=0;
for(int b=0;b<zu;b++)
{
int c=0;
if(rel[b]== len[u])
{
for(int q=0;q<rel[b];q++)
{
if(red[b][q]!=can[u][q]){c=1;break;}
}
if(c== 0)
{
char * relin3 = (char*)malloc(sizeof(char) * 1000);
strcpy(relin3,strs[u]);
// printf("~~%s\n",relin2);
//relin2[len[u]]='\0';
// re[zu][0]=relin2;
re[b][reptr[b]]=relin3;
reptr[b]++;
sig=1;
//printf("^^%s %s\n",strs[u],re[b][reptr[b]-1]);
break;
}
}
}
if(sig==0)
{
char ** relin = (char**)malloc(sizeof(char*) * 1000);
re[zu]=relin;
char * relin2 = (char*)malloc(sizeof(char) * 1000);
strcpy(relin2,strs[u]);
// printf("~~%s\n",relin2);
//relin2[len[u]]='\0';
re[zu][0]=relin2;
// printf("$$$%s\n",strs[u]);
reptr[zu]++;
for(int r=0;r<len[u];r++)
{
red[zu][r]=can[u][r];
}
rel[zu]=len[u];
zu++;
}
}
*returnSize=zu;
// printf("%d\n",zu);
int * rek=(int *)malloc(sizeof(int)*10000);
for(int y=0;y<zu;y++)
{
int * lll = (int*)malloc(sizeof(int));
* lll=reptr[y];
rek[y]=*lll;
// columnSizes[y]=reptr[y];
// printf("@@%d\n",rek[y]);
}
==*columnSizes=rek;==
for(int y=0;y<zu;y++)
{
// columnSizes[y]=20;
// printf("@@%d\n",columnSizes[y]);
/*for(int z=0;z<columnSizes[y];z++)
{
printf("^^%s \n",re[y][z]);
}
*/
// printf("__________\n");
}
// char *** ttt = (char***)malloc(sizeof(char**) * 10000);
columnSizes=NULL;
return re;
// return {["ate","eat","tea"],["nat","tan"],["bat"]};
}