Use push_back() and append functions instead of directly adding a character or a string


#1

string intToString(int x){
string s;
while(x){
char c=x%10+‘0’;
s=c+s; x=x/10;
}
return s;
}

string Solution::countAndSay(int A) {
if(A==1){
return “1”;
} string prev=“1”;
for(int i=2; i<=A; i++){
string curr; prev+=’&’;
char c=prev[0]; int cnt=1;
for(int j=1; j<prev.size(); j++){
if(prev[j]==prev[j-1]){
c=prev[j]; cnt++;
}
else{
string z=intToString(cnt); curr.append(z); curr.push_back©; cnt=1; c=prev[j];
}
}
prev=curr;
}
return prev;
}