Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created February 9, 2018 14:21
Show Gist options
  • Save s4553711/775143f12fd39fa728e55351bd619726 to your computer and use it in GitHub Desktop.
Save s4553711/775143f12fd39fa728e55351bd619726 to your computer and use it in GitHub Desktop.
class Solution {
public:
string countAndSay(int n) {
if (n == 0) return "";
string res = "1";
while (--n) {
string cur = "";
//cout << "new: n: " << n << ", res: " << res << endl;
for(int i = 0; i < res.size(); i++) {
int count = 1;
while((i + 1) < res.size() && res[i] == res[i+1]) {
//cout << "exam> " << i << ", count: " << count << endl;
count++;
i++;
}
//cout << "final> " << i << ", count: " << count << endl;
cur += to_string(count) + res[i];
}
res = cur;
}
return res;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment