c++ solution


  • 0
    B
    class Solution {
    public:
        string generate(string s){
            int count=1;
            char say=s[0];
            string result="";
            if(s=="1") return "11";
            
            for(int i=1;i<s.size();i++){
                if(say!=s[i]) {
                    result=result+to_string(count)+say;
                    say=s[i];
                    count=1;
                }
                else count++;
            }
            result=result+to_string(count)+say;
            return result;
        }
        
        string countAndSay(int n) {
                string s="1";
                for(int i=1;i<n;i++){
                    s=generate(s);
                }
                return s;
        }
    };
    

Log in to reply
 

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