【LeetCode 38_字串_算術運算】Count and Say

QingLiXueShi發表於2015-07-07

 1 string countAndSay(int n)
 2 {
 3     string res;
 4     if (n <= 0)
 5         return res;
 6 
 7     res = "1";
 8     while (n-- > 1) {
 9         int len = res.size();
10         int i, j;
11         string resTmp;
12 
13         for (i = 0; i < len; i = j) {
14             char ch = res[i];
15 
16             for (j = i + 1; j < len; ++j) {
17                 if (ch != res[j])
18                     break;
19             }
20 
21             resTmp = resTmp + (char)(j - i + '0') + ch;
22         }
23         res = resTmp;
24     }
25     return res;
26 }

 

相關文章