Created
January 4, 2013 14:01
-
-
Save pdu/4452782 to your computer and use it in GitHub Desktop.
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
http://leetcode.com/onlinejudge
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) { | |
| string ret = "1"; | |
| while (--n) { | |
| stringstream ss; | |
| int cnt = 0; | |
| for (int i = 0; i < ret.length(); ++i) { | |
| cnt++; | |
| if (i == ret.length() - 1 || ret[i] != ret[i + 1]) { | |
| ss << cnt << ret[i]; | |
| cnt = 0; | |
| } | |
| } | |
| ss >> ret; | |
| } | |
| return ret; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment