class Solution {
public:
string tostr(int x)
{
vector<char> tmp;
while(x)
{
int i=x%10;
tmp.push_back(i+'0');
x/=10;
}
return string(tmp.rbegin(),tmp.rend());
}
string countAndSay(int n) {
string res("1");
int index=1;
while(index<n)
{
string cur;
char curc=res[0];
int curcnt=0;
for(int i=0;i<res.size();++i)
{
if(res[i]!=curc)
{
cur.append(tostr(curcnt));
cur.append(1,curc);
curc=res[i];
curcnt=1;
}
else
{
curcnt++;
}
}
cur.append(tostr(curcnt));
cur.append(1,curc);
res=cur;
index++;
}
return res;
}
};
Count and Say
最新推荐文章于 2019-12-30 05:29:57 发布