-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSolution17.java
More file actions
47 lines (43 loc) · 916 Bytes
/
Copy pathSolution17.java
File metadata and controls
47 lines (43 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package sword_offer;
// page 114 打印从1到最大的n位数
public class Solution17 {
// 数字排列方法
public static void maxOfND(int n) {
if (n < 0) {
return;
}
char[] num = new char[n];
for (int i = 0; i < 10; i++) {
num[0] = (char) (i + '0');
printNumRec(num, n, 0);
}
}
// 递归部分
public static void printNumRec(char[] num, int n, int index) {
if (index == n - 1) {
printNum(num);
return;
}
for (int i = 0; i < 10; i++) {
num[index + 1] = (char) (i + '0');
printNumRec(num, n, index + 1);
}
}
// 以阅读习惯输出
public static void printNum(char[] num) {
boolean isBegin0 = true;
for (int i = 0; i < num.length; i++) {
if (isBegin0 && num[i] != '0') {
isBegin0 = false;
}
if (!isBegin0) {
System.out.print(num[i]);
}
}
System.out.println();
}
// 测试
public static void main(String[] args) {
maxOfND(5);
}
}