代码改变世界

leetcode - Count and Say

2013-03-28 10:52  张汉生  阅读(112)  评论(0编辑  收藏  举报

题目描述:点击此处

 1 class Solution {
 2 public:
 3   string genNext(string cur){
 4     string rlt = "";
 5     char t[20];
 6     int length = cur.length();
 7     int i = 0;
 8     char c = cur.at(0);
 9     int len = 0;
10     while(i<length){
11       while (i<length && cur.at(i)==c){
12         len++;
13         i++;
14       }
15       sprintf(t, "%d%c", len, c);
16       rlt += t;
17       len =0;
18       if (i<length){
19         c = cur.at(i);
20       }
21     }
22     return rlt;
23   }
24   string countAndSay(int n) {
25     // Start typing your C/C++ solution below
26     // DO NOT write int main() function
27     string rlt = "1";
28     while (--n > 0){
29       rlt = genNext(rlt);
30     }
31     return rlt;
32   }
33 };