Skip to content

Instantly share code, notes, and snippets.

@pdu
Created January 4, 2013 14:01
Show Gist options
  • Select an option

  • Save pdu/4452782 to your computer and use it in GitHub Desktop.

Select an option

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
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