string countAndSay(string s)
{
string result;
int index1 = 0, index2 = 0, count = 0;
while(index1 < s.length())
{
if (s[index2] == s[index1])
{
count++;
index2++;
}
if (index2 == s.length() || s[index2] != s[index1])
{
stringstream ss;
ss<<count;
result += ss.str();
result += s[index1];
index1 = index2;
count = 0;
}
}
return result;
}
string countAndSay(int n)
{
string str = "1";
for (int i = 1; i < n; i++)
{
str = countAndSay(str);
}
return str;
}
[LeetCode] Count and Say
最新推荐文章于 2019-08-14 16:27:56 发布