class Solution {
public:
string countAndSay(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string s = "1";
for (int i = 2; i <= n; ++i)
{
string nextS = "";
char prevC = '-';
int count = 0;
for (int j = 0; j < s.size(); ++j)
{
if (s[j] != prevC)
{
if (prevC != '-')
{
nextS = nextS + (char)('0' + count);
nextS = nextS + prevC;
}
prevC = s[j];
count = 1;
}
else
{
++count;
}
}
nextS = nextS + (char)('0' + count);
nextS = nextS + prevC;
s = nextS;
}
return s;
}
};[Leetcode] Count and Say
最新推荐文章于 2020-06-07 06:58:02 发布
本文介绍了一个使用C++实现的Count and Say算法。通过迭代构造字符串的方式,实现了LeetCode第38题的要求,即生成第n项的Count and Say序列。

4478

被折叠的 条评论
为什么被折叠?



