leetcode 357 Count Numbers with Unique Digits
z

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.

Example:

1
2
3
4
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class CountNumberswithUniqueDigits {
/*
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10^n.
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
*/
public int countNumbersWithUniqueDigits(int n) {
if (n<1){
return 1;
}
else if (n==1){
return 10;
}
else if (n>10){
n = 10;
}
return countNumbersWithUniqueDigits(n-1) + (countNumbersWithUniqueDigits(n-1) - countNumbersWithUniqueDigits(n-2)) * (10-(n-1));
}
}```