C recusive solution


  • 0

    char* countAndSay(int n) {
    char *now;
    if (n == 1)
    {now= "1";}
    else
    {
    now = "";
    char before;
    before = countAndSay(n-1);
    char c;
    char compte[10];
    c= before[0];
    int i = 1,count = 1;
    while (c != '\0')
    {
    if (before[i] != c)
    {
    sprintf(compte, "%d", count);
    char
    temp= (char *)malloc(strlen(now) + 12);
    temp[0] = '\0';
    strcat(temp,now);
    strcat(temp,compte);
    temp[strlen(temp)] = c;
    temp[strlen(temp) +1] = '\0';
    now = temp;
    c = before[i];
    count = 1;
    }
    else
    {
    count ++;
    }
    i++;
    }
    }
    return now;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.