题目描述
解题思路
class Solution {
public int[] res;
public int indexOfRes = 0;
public int[] printNumbers(int n) {
res = new int[(int)Math.pow(10, n) - 1];
char[] num = new char[n];
backtrack(num, 0);
return res;
}
public void backtrack(char[] num, int indexOfNum) {
if (indexOfNum == num.length) {
save(num);
return;
}
for (char i = '0'; i <= '9'; i++) {
num[indexOfNum] = i;
backtrack(num, indexOfNum + 1);
}
}
public void save(char[] num) {
boolean isBegin0 = true;
String stringOfNum = "";
for (int i = 0; i < num.length; i++) {
if (isBegin0 && (num[i] != '0'))
isBegin0 = false;
if (!isBegin0) {
stringOfNum += num[i];
}
}
if (!isBegin0) {
res[indexOfRes++] = Integer.valueOf(stringOfNum);
}
}
}