38. Count and Say
递归实现,根据深层递归的结果进行数数,生成此次结果,传给上层
最后出错是忘了初始化count变量
/* 递归实现 */ if (n == 1) return "1"; //最后加上一个*是为了处理字符串末尾的字符 String str = countAndSay(n-1)+"*"; int count = 1; String s = ""; for (int i = 0; i < str.length()-1; i++) { if (str.charAt(i) == str.charAt(i+1)) { count++; } else { s += count+""+str.charAt(i); count = 1; } } return s;