Count Numbers with Unique Digits
Thoughts
Code
class Solution {
public int countNumbersWithUniqueDigits(int n) {
int res = 0;
if (n == 0) {
res += 1;
return res;
}
int[] f = new int[n + 1];
f[0] = 1;
f[1] = 9;
for (int i = 2, j = 9; i <= n && j > 0; i++, j--) {
f[i] = f[i - 1] * j;
}
for (int i = 0; i <= n; i++) {
res += f[i];
}
return res;
}
}Analysis
Last updated