这题真的坑爹啊,不明白为什么会产生万大小的数据啊。
/**
* 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 c[20][20];
bool judge(char* s,int head,int tail){
while(head<=tail){
if(s[head]!=s[tail]) return false;
head++;
tail--;
}
return true;
}
void dfs(char* s,char*** array,int* returnSize,int* column,int index,int count,int n){
if(index>n) return;
if(index==n){
column[*returnSize]=count;
for(int i=0;i<count;i++) strcpy(array[*returnSize][i],c[i]);
(*returnSize)++;
return ;
}
for(int i=0;i<n;i++){
int tail=index+i;
if(tail<n){
if(judge(s,index,tail)){
for(int j=index;j<=tail;j++) c[count][j-index]=s[j];
dfs(s,array,returnSize,column,tail+1,count+1,n);
for(int j=index;j<=tail;j++) c[count][j-index]=0;
}
}
}
}
char*** partition(char* s, int* returnSize, int** returnColumnSizes) {
*returnSize=0;
char*** array=(char***)malloc(sizeof(char**)*20000);
for(int i=0;i<20000;i++) array[i]=(char**)malloc(sizeof(char*)*17);
for (int i = 0; i < 20000; i++) {
for (int j = 0; j < 17; j++) {
array[i][j] = (char*)malloc(sizeof(char) * 17);
}
}
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
c[i][j]=0;
}
}
int* column=(int*)malloc(sizeof(int)*20000);
dfs(s,array,returnSize,column,0,0,strlen(s));
*returnColumnSizes=column;
return array;
}
结果:
标签:分割,20,returned,returnSize,int,131,array,回文 From: https://www.cnblogs.com/llllmz/p/18062681