0ms Cpp Solution


  • 0
    K

    class Solution {

    public:

    string generateNext(string str){
        int count=0,i=0;
        string result="";
        while(str[i]){
            count=1;
            while(str[i]==str[i+1]){
                count++;
                i++;
            }
            result.push_back(count+'0');
            result.push_back(str[i]);
            i++;
        }
        cout<<result<<endl;
        return result;
    }
    string countAndSay(int n) {
        string result="1";
        string def="1";
        if(n==1)
            return def;
        for(int i=1;i<n;i++)
            result=generateNext(result);
        return result;
    }
    

    };


  • 1
    S

    It's wrong if there is a sub-sequence longer than 10 with the same char.

    For example:
    11111111111111122

    result.push_back(count+'0');
    

    This code will not work normally


Log in to reply
 

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