【leetcode】131. 分割回文串

 

void recursion(char* s,int* returnSize,int* col,int* oodhash,int* evenhash,int pst,int start,int len,char** temp,char*** arr){
    if(start==len){
        arr[(*returnSize)]=(char**)calloc(pst+1,sizeof(char*));
        memcpy(arr[(*returnSize)],temp,sizeof(int*)*pst);
        col[(*returnSize)]=pst;
        (*returnSize)++;
        return;
    }    
    for (int i=start; i<len; i++)
    {
        int mid=(i+start)/2;
        char* buffer=(char*)calloc(20,sizeof(char));
        if( (i-start+1)%2 ){
            if ( start>=mid-oodhash[mid] && i<=mid+oodhash[mid] )
            {
                memcpy(buffer,&s[start],i-start+1);
                temp[pst]=buffer;
            }
            else
                continue;            
        }
        else if( (i-start+1)%2==0 ){
            if (start>=mid-evenhash[mid] && i<=mid+1+evenhash[mid])
            {
                memcpy(buffer,&s[start],i-start+1);
                temp[pst]=buffer;
            }
            else
                continue;            
        }
        recursion(s,returnSize,col,oodhash,evenhash,pst+1,i+1,len,temp,arr);
    }
}

char *** partition(char * s, int* returnSize, int** returnColumnSizes){
    *returnSize=0;
    *returnColumnSizes=(int*)calloc(50000,sizeof(int));
    char*** arr=(char***)calloc(50000,sizeof(char**));
    int oodhash[100]={0};
    int evenhash[100]={0};
    char* temp[100];
    int i, j, len=strlen(s), cnt;
    for (i=0; i<len; i++)
    {
        j=1; cnt=0;
        while(i-j>=0 && i+j<len && s[i-j]==s[i+j]){
            cnt++;
            j++;
        }
        oodhash[i]=cnt;
        if(i!=len-1){
            j=0; cnt=-1;
            while(i-j>=0 && i+1+j<len && s[i-j]==s[i+1+j]){
                cnt++;
                j++;
            }
            evenhash[i]=cnt;
        }
    }
    recursion(s,returnSize,*returnColumnSizes,oodhash,evenhash,0,0,len,temp,arr);
    return arr;
}

 

posted @ 2020-12-20 21:29  温暖了寂寞  阅读(97)  评论(0编辑  收藏  举报