class Solution {
public:
string countAndSay(int n) {
if(n==1)return "1";
string previous=countAndSay(n-1); //递归调用
string result= "";
int count=1; //count 计数
for(int i=0;i<previous.size();i++){
if(previous[i]==previous[i+1]){ //i+1可以,i++不行???
count++; //原因是i++改变了i的值,而i+1并没有改变
}
else{
result+=to_string(count)+previous[i];
count=1;
}
}
return result;
}
};
11-22
485
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-09
229
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-09
810
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)