My simple C++ solution


  • 0
    N

    class Solution {

    public:
        string countAndSay(int n) {
        if( n == 1) {
            return "1";
        }
        
        string prev = countAndSay(n - 1);
        string res;
        int i = 0, j = 1, len = prev.size();
        for(; i < prev.size(); i++) {
            if((i == len - 1) || (prev[i] != prev[i + 1])) {
               res += (j + '0');
               res += prev[i];
               j = 1;
            } else {
               j++;
            }
        }
        
        return res;
    }
    

    };


Log in to reply
 

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