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;
    }
};
posted @ 2018-09-14 20:08  narjaja  阅读(112)  评论(0编辑  收藏  举报