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         // Note: The Solution object is instantiated only once and is reused by each test case.
 4         if(n < 1) return null;
 5         StringBuffer first = new StringBuffer();
 6         StringBuffer second = new StringBuffer();
 7         first.append(1);
 8         int a = 1;
 9         while(a < n){
10             char per = first.charAt(0);
11             int countf = 1;
12             for(int i = 1; i < first.length(); i ++){
13                 if(per == first.charAt(i)){
14                     countf ++;
15                 }else{
16                     second.append(countf);
17                     second.append(per);
18                     per = first.charAt(i);
19                     countf = 1;
20                 }
21             }
22             second.append(countf);
23             second.append(per);
24             first = second;
25             second = new StringBuffer();
26             a ++;
27         }
28         return first.toString();
29     }
30 }

 第二遍:

 1 public class Solution {
 2     public String countAndSay(int n) {
 3         // Note: The Solution object is instantiated only once and is reused by each test case.
 4         if(n < 1) return null;
 5         String result = "1";
 6         for(int i = 0; i < n - 1; i ++){
 7             StringBuffer sb = new StringBuffer();
 8             int count = 1;
 9             char c = result.charAt(0);
10             for(int j = 1; j < result.length(); j ++){
11                 if(c != result.charAt(j)){
12                     sb.append(count);
13                     sb.append(c);
14                     c = result.charAt(j);
15                     count = 1;
16                 }else{
17                     count ++;
18                 }
19             }
20             sb.append(count);
21             sb.append(c);
22             result = sb.toString();
23         }
24         return result;
25     }
26 }

 第三遍:

 1 public class Solution {
 2     public String countAndSay(int n) {
 3         String result = "1";
 4         if(n <= 1) return result;
 5         StringBuilder sb = new StringBuilder();
 6         while(n > 1){
 7             int count = 0;
 8             sb = new StringBuilder();
 9             for(int i = 0; i < result.length(); i ++){
10                 if(i == 0 || result.charAt(i) == result.charAt(i - 1)) count ++;
11                 else{
12                     sb.append(count);
13                     sb.append(result.charAt(i - 1));
14                     count = 1;
15                 }
16             }
17             sb.append(count);
18             sb.append(result.charAt(result.length() - 1));
19             result = sb.toString();
20             n --;
21         }
22         return result;
23     }
24 }

 

posted on 2013-10-12 12:51  Step-BY-Step  阅读(237)  评论(0编辑  收藏  举报

导航