420. 报数

class Solution {
public:
    /**
     * @param n: the nth
     * @return: the nth sequence
     */
    string countAndSay(int n) {
        // write your code here
        string s = "1";
        if (n==1) return s;
        while(--n) {
            string re = "";
            int l = s.size();
            int j = 0;
            while (j < l) {
                int count = 1;
                char tmp = s[j];
                for (int i=j+1; i<l; ++i) {
                    if (s[i] == tmp) count++;
                    else break;
                }
                re = re + to_string(count);
                re = re + tmp;
                j+=count;
            }
            s = re;
        }
        return s;
    }
};
原文地址:https://www.cnblogs.com/narjaja/p/9648583.html