A Simple C++ Solution


  • 0
    S

    class Solution {
    public:
    string countAndSay(int n) {

        string s ="11";
        int t=2;
        if(n==1)
            return "1";
        if(n==2)
            return "11";
        string kk = "";
        while(t!=n){
            kk = "";
            for(int i=0;i<s.length();i++){
                int j=1;
                char jj = s[i];
                while(s[i]==s[i+1]){
                    i++;
                    j++;
                }
                string ll;
           
                ll = to_string(j);
                ll.push_back(jj);
            
               kk =  kk + ll;
                
            }
            s = kk;
            t++;
        }
        return s;
    }
    

    };


Log in to reply
 

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