注意:int 转 string 要用 to_string
字符串的拼接要用 +=
字符串后接char 要用 push_back
class Solution {
public:
string countAndSay(int n) {
string mystring = "1";
for(int i = 0; i < n-1; i++){
mystring = say(mystring);
}
return mystring;
}
private:
string say(string s){
if(s.size() == 1) return "1" + s;
string res = "";
int cur = 1;
for(int i = 0; i < s.size();){
if(s[i] == s[i + 1]){
cur++;
i++;
}
else{
res+=(to_string(cur));
res.push_back(s[i]);
i++;
cur = 1;
}
}
return res;
}
};