Question Definition
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99]
)
Java Solution
class Solution {
public int countNumbersWithUniqueDigits(int n) {
if (n == 0) return 1;
int res = 0;
for (int i = 1; i <= n; ++i) {
res += count(i);
}
return res;
}
private int count(int k) {
if (k < 1) return 0;
if (k == 1) return 10;
int res = 1;
for (int i = 9; i >= (11 - k); --i) {
res *= i;
}
return res * 9;
}
}
Comments