模拟。记得把个数转化为字符串。(可能是两位数或以上)。
class Solution {
public:
string countAndSay(int n) {
string str = "1", ans = "1";
int num = 1;
while(num < n){
char c = str[0];
ans.clear();
int cnt = 1;
for(int i = 1; i <= str.size(); i++){
if(i == str.size() ||str[i] != c){
ans += int2str(cnt);
ans += c;
c = str[i];
cnt = 1;
}
else cnt++;
}
str = ans;
num++;
}
return ans;
}
string int2str(int n){
string res;
while(n){
res = char((n%10)+'0') + res;
n /= 10;
}
return res;
}
};