Easy understand C++ solution


  • 0
    S

    class Solution {
    public:
    string countAndSay(int n) {
    if(n==1) return "1";
    if(n>1){
    string temps=countAndSay(n-1);
    string ans;
    int len=temps.length();
    int i=0;
    int count=1;
    while(i<len){
    if(i+1<len&&temps[i]==temps[i+1]) count++;
    else {ans+=to_string(count)+temps[i];count=1;}
    i++;}
    return ans;
    }
    }
    };


Log in to reply
 

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