static int[] goodNums = new int[5680000]; static int p = 0; static { for (int i = 1; i <= 9; i++) { dfs(0, i, 0, 0); } goodNums[p++] = 1023456789; } static void dfs(int pos, int n, int mask, int curNum) { if (pos == n) { goodNums[p++] = curNum; return; } for (int i = 0; i <= 9; i++) { if (i == 0 && pos == 0) { continue; } if (((mask >> i) & 1) == 0) { dfs(pos + 1, n, mask | (1 << i), curNum * 10 + i); } } }