首页 > 代码库 > Leetcode: Count and Say

Leetcode: Count and Say

一次过

 1 public class Solution {
 2     public String countAndSay(int n) {
 3         if (n <= 0) return "";
 4         int i = 1;
 5         String current = "1";
 6         while (i < n){
 7             current = getnext(current);
 8             i++;
 9         }
10         return current;
11     }
12     
13     public String getnext(String current){
14         int j = 0, count = 0;
15         char st = current.charAt(0);
16         StringBuffer result = new StringBuffer();
17         while (j < current.length()){
18             if (current.charAt(j) == st){
19                 count++;
20                 j++;
21             } else{
22                 result.append(count);
23                 result.append(st);
24                 st = current.charAt(j);
25                 count = 1;
26                 j++;
27             }
28         }
29         result.append(count);
30         result.append(st);
31         return result.toString();
32     }
33 }