a C inplement


  • 0
    W
    char* countAndSay(int n) {
        
        char* s;
        char* result;
        int n_count;
        int r_count=0;
        int i=0;
        
        result = (char*)malloc(5000*sizeof(char));
        
        if(n == 1) return "1";
        else
        {
            s = countAndSay(n-1);
            while(*(s+i) != '\0')
            {
                n_count=1;
                while(*(s+i) == *(s+i+1))
                {
                    n_count++;
                    i++;               
                }
                *(result+r_count) = n_count+'0';
                r_count++;
                *(result+r_count) = *(s+i);
                r_count++;     
                i++;
            }
            result[r_count] = '\0';
            i=0;
            return result;
        }
    }
    

Log in to reply
 

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