Created
February 9, 2018 14:21
-
-
Save s4553711/775143f12fd39fa728e55351bd619726 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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