【LeetCode 38】报数

题目链接

【题解】

模拟题

【代码】

class Solution {
public:
    string inttostr(int x){
        string temp="";
        while (x>0){
            char key = (x%10)+'0';
            temp= key+temp;
            x/=10;
        }
        return temp;
    }
    string countAndSay(int n) {
        string a = "1";
        string b = "";
        for (int i = 0;i < n-1;i++){
            int len = a.size();
            for (int j = 0;j < len;j++){
                int k = j;
                while (k+1<len && a[k+1]==a[k]) k++;
                int num = k-j+1;
                string snum = inttostr(num);
                snum=snum+a[k];
                b = b+snum;
                j= k;
            }
            a = b;
            b = "";
        }
        return a;
    }
};
posted @ 2019-11-13 11:04  AWCXV  阅读(84)  评论(0编辑  收藏  举报