Pretty straight forward. count numbers and put char.

 1 class Solution {
 2 public:
 3     string getNext(string s) {
 4         ostringstream oss;
 5         char rec = s[0];
 6         int len = s.size(), num = 1;
 7         for (int i = 1; i < len; i++) {
 8             if (rec != s[i]) {
 9                 oss << char(num + '0') << rec;
10                 rec = s[i];
11                 num = 1;
12             } else num++;
13         }
14         oss << char(num + '0') << rec;
15         return oss.str();
16     }
17     string countAndSay(int n) {
18         string result = "1";
19         for (int i = 2; i <= n; i++) {
20             result = getNext(result);
21         }
22         return result;
23     }
24 };

 

posted on 2015-03-19 07:20  keepshuatishuati  阅读(152)  评论(0编辑  收藏  举报