C++ Accepted Solution


  • 0
    C
     string sequence(string n, int count) {
            if(count == 1){
                return n;
            }
            
            int array[10] = { 0 };
            string temp = "";
            
            for(int i =0; i < n.length(); i++){
                array[int(n[i]) - 48]++;
                
                if(n[i+1] != n[i]){
                    temp += to_string(array[int(n[i]) - 48]);
                    array[int(n[i]) - 48] = 0;
                    temp += n[i];
                }
            }
            count--;
            
            if(count != 1)
                return sequence(temp, count);
            else
                return temp;
        }
    
        string countAndSay(int n) {
            int first = 1;
            
            string temp = sequence(to_string(first), n);
            
            return temp;
        }
    
    
    
    
    

Log in to reply
 

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