420. Count and Say
string countAndSay(int n) {
// write your code here
string res = "1";
int cnt = 0;
char start;
for(int i = 2; i <= n; i++){
string temp = "";
int j = 0;
while(j < res.size()){
cnt = 1;
start = res[j];
while(j < res.size() - 1 && res[j] == res[j + 1]){
cnt++;
j++;
}
temp += (to_string(cnt) + (start));
++j;
}
//temp += (to_string(cnt) + to_string(start));
res = temp;
}
return res;
}
Comments
Post a Comment