代码实现
public static void main(String[] args) {
String[] a = new String[20];
a[0] = "1";
for (int i = 1; i < a.length; i++) {
String s = String.valueOf(a[i - 1]);
char[] chars = s.toCharArray();
int index = 0;
StringBuilder result = new StringBuilder();
while (index < chars.length) {
int times = 1;
char p = chars[index];
int t = index + 1;
while (t < chars.length) {
if (p == chars[t++]) {
times++;
} else {
t--;
break;
}
}
index = t;
result.append(times).append(p);
}
a[i] = result.toString();
}
System.out.println(Arrays.toString(a));
}
输出
1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211, 31131211131221…