[leetcode_38]Count and Say

模拟题,迭代直到n即可。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
    string SayStep(string str) {
        string strstep = "";
        int index = 0;
        while(index < str.length()) {
            char c = str[index];
            int count_c = 1;
            int i;
            for(i = index+1;i < str.length();i++) {
                if(c == str[i])count_c++;
                else break;
            }
            index = i;
            strstep.push_back(count_c + '0');
            strstep.push_back(c);
        }
        return strstep;
    }
    string countAndSay(int n) {
        string str = "1";
        for(int i = 1; i < n;i++) {
            str = SayStep(str);
        }
        return str;
    }
};
Licensed under CC BY-NC-SA 4.0