leetcode38外观数列C++

1、题目

https://leetcode-cn.com/problems/count-and-say/

2、题意

题解1:简单模拟 每次算与当前区间相等的个数 最后更新字符串;

class Solution {
public:
    string countAndSay(int n) {
        string last = "1";
        for(int t=2;t<=n;t++)
        {
            string result = "";
            for(int i=0;i<last.size();)
            {
                int j = i+1;
                while(j<last.size()&&last[j]==last[i])
                    j++;
                int total = j-i;
                result+=to_string(total)+last[i];
                i = j;
            }
            last = result;
        }
        return last;
    }
};

你可能感兴趣的:(leetcode)