Leetcode 38. 外观数列

迭代法如下

char * countAndSay(int n){
    char * res = (char *)malloc(sizeof(char)*10000);
    char * res0 = (char *)malloc(sizeof(char)*10000);
    int i,j,d,num;
    res0[0]='1';
    res0[1]='\0';
    strcpy(res,res0);
    for(i=0;i<n-1;i++)
    {
        j=0;
        d=0;
        while(res0[j]!='\0')
        {
            num=0;
            while(res0[j+num]==res0[j])
            {
                num++;
            }    
            res[d++]=num+48;
            res[d++]=res0[j];
            j+=num;
        }
        res[d]='\0';
        strcpy(res0,res);
    }
    return res;
}

猜你喜欢

转载自www.cnblogs.com/wzmm/p/12389981.html