class Solution {
public:
string countAndSay(int n) {
string res="1";
while(--n){
res=say(res);
}
return res;
}
string say(string s){
string res;
char c=s[0];
string str;
int count=0;
for(int i=0;i<s.size();i++){
if(s[i]==c){
count++;
}
else{
char tmp[20];
sprintf(tmp,"%d",count);
str=tmp;
res+=str;
res+=c;
c=s[i];
count=1;
}
}
char tmp[20];
sprintf(tmp,"%d",count);
str=tmp;
res+=str;
res+=c;
return res;
}
};
LeetCode - 38 Count and Say
最新推荐文章于 2021-03-17 11:00:30 发布