简单的排列组合问题,数字一共10个,所以不重复的数最多占10位,也就是0<=x<10^10,只需要考虑n<=10的情况,从n位数到1一位数依次遍历,统计。
最后一个0单独考虑。
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
int ans = 0;
if (n > 10) {
n = 10;
}
for (int i = n; i > 0; --i) {
int now = 1;
for (int j = i; j > 0; --j) {
if (j == i) {
now *= 9;
} else {
now *= (10 + j - i);
}
}
ans += now;
}
return ans + 1;
}
};