【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;
    }
};

猜你喜欢

转载自www.cnblogs.com/AWCXV/p/11847491.html