- Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
Input: 2
Output: 91
Explanation: The answer should be the total numbers in the range of 0 ≤ x < 100,
excluding 11,22,33,44,55,66,77,88,99
代码
class Solution {
public int countNumbersWithUniqueDigits(int n) {
if( n == 0 ){
return 1;
}
if( n == 1 ){
return 10;
}
if( n >= 10 ){
return 0;
}
int current = 81; // n == 2; f(n) = f(n-1)*(11-n);
int total = 91; // n == 2;
for(int i = 3 ; i <= n; i++){
current *= (11-i);
total += current;
}
return total;
}
}