class Solution {
public:
string countAndSay(int n)
{
if(n<=0)
return "";
string result="1";
for(int i=2;i<=n;++i)
{
string temp="";
int start=0;
int end=0;
for(int j=1;j<result.size();++j)
{
if(result[j]==result[j-1])
{
end=j;
}
else
{
temp+=to_string(end-start+1)+result[j-1];
start=j;
end=j;
}
}
temp+=to_string(end-start+1)+result[end];
result=temp;
}
return result;
}
};
Count and Say
最新推荐文章于 2024-12-21 10:31:21 发布