Iterate and recursive solution


  • 0
    B
    class Solution {
    public:
        string countAndSay(int n) {
            string s = "1";
            while (--n) {
                s = f(s);
            }
            return s;
        }
    
        string countAndSay_recur(int n) {
            if (n == 1)
                return "1";
            string s = countAndSay_recur(n-1);
            return f(s);
        }
    
    private:
        // count-and-say the string 'n', return the result
        string f(string& n) {
            string res;
    
            for (int i = 0; i < n.size(); ) {
                int times = 1;
                char c = n[i];
                // find duplicates
                for (++i; i < n.size() && n[i] == c; ++i)
                    ++times;
    
                res += times+'0';
                res += c;
            }
            return res;
        }
    };

Log in to reply
 

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