Easiest c++ code using recursive


  • 0
    H
    enter code here string countAndSay(int n) {
        if (n==1) return "1";
        string tmp = countAndSay(n-1) + ' ';
        string res = "";
        int count = 1;
        for(int i = 1; i < tmp.size(); ++i){
            if(tmp[i] == tmp[i-1]) count++;
            else {
                stringstream ss;
                ss << count;
                res += ss.str() + tmp[i-1];
                count = 1;
            }
        }
        return res;
    }

Log in to reply
 

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