Leetcode每日一题:38.Count and Say(外观数列)_i++
看题目着实难懂,评论老哥帮忙找答案,可以这么理解:
题目的意思是对序列前一个数进行报数,数列第一项为1,那第二项就报第一项的有1个1,输出11,然后第三项就在第二项的基础上报数,第二项是11,第三项不就是2个1么,然后输出21
Leetcode每日一题:38.Count and Say(外观数列)_i++_02

string countAndSay(int n)
{
    string res = "11";
    if (n == 1)
        return "1";
    if (n == 2)
        return "11";
    for (int i = 3; i <= n; i++)
    {
        string resNow = "";
        int lenRes = res.size();
        for (int i = 0; i < lenRes; i++)
        {
            int count = 1;
            while (res[i] == res[i + 1] && i < lenRes)
            {
                count++;
                i++;
            }
            resNow.push_back(count + '0');
            resNow.push_back(res[i]);
        }
        res=resNow;
    }
    return res;
}