38. 报数
https://leetcode-cn.com/problems/count-and-say/description/
package com.test; /** * @Author stono * @Date 2018/8/20 上午11:11 * */ public class Lesson038 { public static void main(String[] args) { int n = 6; String s = countAndSay(n); System.out.println(s); } public static String countAndSay(int n) { if (n - 1 == 0) { return "1"; }else { // 使用递归进行计算 String n2 = countAndSay(n-1); char[] chars = n2.toCharArray(); StringBuilder res = new StringBuilder(); // 找出与第i个所有相同的数字,进行计数统计 for (int i=0;i<chars.length;i++) { char aChar = chars[i]; int count = 1; while (i + 1 < chars.length && chars[i + 1] - aChar == 0) { count ++; i++; } res.append(count).append(aChar); } return res.toString(); } } }
还可以用模式空间:
https://blog.csdn.net/wanglelelihuanhuan/article/details/51591809
但是不是特别明白;