https://leetcode-cn.com/problems/count-and-say/
报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下:
- 1
- 11
- 21
- 1211
- 111221
字符串模拟,一步一步照着题意来呗
class Solution {
public:
string countAndSay(int n) {
string res = "1";
n--;
while(n--)
{
int len = res.size();
string temp = "";
int begin = 0;
int end = 0;
while(1)
{
if( end == len-1 ){
temp += ('0' + end-begin+1);
temp += res[begin];
break;
}
else if( res[end+1] == res[begin] ){
end++;
}
else{
temp += ('0' + end-begin+1);
temp += res[begin];
begin = end+1;
end = begin;
}
}
res = temp;
//cout << res << endl;
}
return res;
}
};