38. Count and Say
没什么要说的,就是要好好看题= =…………我看错了两遍,蠢cry
1 public String countAndSay(int n) { 2 if(n == 0) { 3 return ""; 4 } 5 if(n == 1) { 6 return "1"; 7 } 8 String nth = String.valueOf(11); 9 for(int i = 0; i < n - 2; i++) { 10 nth = helper(nth); 11 } 12 return nth; 13 14 } 15 16 private String helper(String n) { 17 StringBuilder sb = new StringBuilder(); 18 StringBuilder resString = new StringBuilder(); 19 sb.append(n); 20 int i = 0; 21 while(i < sb.length()) { 22 char x = sb.charAt(i); 23 i++; 24 int cnt = 1; 25 while(i < sb.length() && sb.charAt(i) == x) { 26 cnt ++; 27 i++; 28 } 29 resString.append(cnt); 30 resString.append(x); 31 } 32 return resString.toString(); 33 }
bug记录
1.第一次看错是看成给一个数字,转换成如果read
2.第二次看错,看成给定一个read出来的值如何转化成原来的值
其实是求第nth个
3.注意前面n = 1&2的时候已经处理过了,所以第九行处是i<n-2