38.Count and Say

原文链接: http://www.cnblogs.com/smallredness/p/10675036.html
class Solution {
public:
    string countAndSay(int n) {
        if (n <= 0) return "";
        string res = "1";
        while (--n) {
            string cur = "";
            for (int i = 0; i < res.size(); ++i) {
                int cnt = 1;
                while (i + 1 < res.size() && res[i] == res[i + 1]) {
                    ++cnt;
                    ++i;
                }
                cur += to_string(cnt) + res[i];
            }
            res = cur;
        }
        return res;
    }
};

转载于:https://www.cnblogs.com/smallredness/p/10675036.html

猜你喜欢

转载自blog.csdn.net/weixin_30716725/article/details/94942585