Count and Say

recursion programming

 1 public class Solution {
 2     public String countAndSay(int n) {
 3         // IMPORTANT: Please reset any member data you declared, as
 4         // the same Solution instance will be reused for each test case.
 5         if(n == 1)
 6             return "1";
 7         char[] mychar = countAndSay(n-1).toCharArray();
 8         String result = "";
 9         char tmp = ' ';
10         int count = 0;
11         for(int i = 0; i < mychar.length; i++)
12         {
13             if(mychar[i] != tmp)
14             {
15                 if(tmp == ' ')
16                 {
17                     tmp = mychar[i];
18                     count = 1;
19                 }
20                 else
21                 {
22                     result += String.valueOf(count) + tmp;
23                     tmp = mychar[i];
24                     count = 1;
25                 }
26             }
27             else
28             {
29                 count++;
30             }
31         }
32         result += String.valueOf(count) + tmp;
33         return result;
34     }
35 }
原文地址:https://www.cnblogs.com/jasonC/p/3408763.html