Count and Say [LeetCode]

The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.

Given an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

Summary: Just like Fibonacci number

 1     string toString(int n) {
 2         string str;
 3         while(true){
 4             str.insert(str.begin(), (n % 10) + 48);
 5             n = n / 10;
 6             if(n == 0)
 7                 break;
 8         }
 9         return str;
10     }
11     
12     string countAndSay(int n) {
13         string seq;
14         seq.push_back('1');
15         for(int i = 2; i <= n; i ++) {
16             string tmp;
17             char current_char = seq[0];
18             int count = 1;
19             for(int j = 1; j < seq.size(); j ++) {
20                 if(seq[j] == current_char){
21                     count ++;
22                 }else{
23                     tmp += toString(count);
24                     tmp += current_char; 
25                     current_char = seq[j];
26                     count = 1;
27                 }
28             }
29             tmp += toString(count);
30             tmp += current_char;
31             
32             seq = tmp;
33         }
34         return seq;
35     }
原文地址:https://www.cnblogs.com/guyufei/p/3408442.html