https://leetcode.com/problems/count-numbers-with-unique-digits/description/
n digits: 9*8*7*...
n-1 digits: 9*8*...
1 digits nonzero: 9
1 digits zero: 1
int countNumbersWithUniqueDigits(int n) {
if(n==0) return 1;
if(n>10) return 0;
int res = 1;
for(int i=1; i<n; i++) {
int t = 1;
for(int j=1; j<=i; j++) {
t *= (10-j);
}
res += t;
}
res *= 9;
return res+1;
}
No comments:
Post a Comment