# 解题思路

class Solution {
int[] res;
int count = 0;

public int[] printNumbers(int n) {
res = new int[(int)Math.pow(10, n) - 1];
for(int digit = 1; digit < n + 1; digit++){
for(char first = '1'; first <= '9'; first++){
char[] num = new char[digit];
num[0] = first;
dfs(1, num, digit);
}
}
return res;
}

private void dfs(int index, char[] num, int digit){
if(index == digit){
res[count++] = Integer.parseInt(String.valueOf(num));
return;
}
for(char i = '0'; i <= '9'; i++){
num[index] = i;
dfs(index + 1, num, digit);
}
}
}


# 代码

class Solution {
int[] res;
int nine = 0, count = 0, start, n;
char[] num, loop = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
public int[] printNumbers(int n) {
this.n = n;
res = new int[(int)Math.pow(10, n) - 1];
num = new char[n];
start = n - 1;
dfs(0);
return res;
}
void dfs(int x) {
if(x == n) {
String s = String.valueOf(num).substring(start);
if(!s.equals("0")) res[count++] = Integer.parseInt(s);
if(n - start == nine) start--;
return;
}
for(char i : loop) {
if(i == '9') nine++;
num[x] = i;
dfs(x + 1);
}
nine--;
}
}


12-01 23
02-06 50
07-04 152
04-16 77
12-08
12-31