Count and Say

Count and Say

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.

 1 public class Solution {
 2     public String countAndSay(int n) {
 3         String result = "1";
 4 
 5         for(int i = 1; i < n; i++){
 6             char array_char[] = result.toCharArray();
 7             int count = 1;//record count
 8             result = new String();//暂存
 9             for(int j = 0; j < array_char.length; ){//start read
10                 int k = j + 1;
11                 
12                 while(k < array_char.length && array_char[k] == array_char[j])
13                 {
14                     count++;
15                     k++;
16                 }//equal count ++
17                 
18                 result += String.valueOf(count);
19                 result += array_char[j];
20                 j = k;
21                 count = 1;
22                 
23             }
24         }
25         
26         return result;
27     }
28 }

按照定义来做

原文地址:https://www.cnblogs.com/luckygxf/p/4085904.html