点击打开链接
//f(1) = 10, ..., f(k) = 9 * 9 * 8 * ... (9 - k + 2) [The first factor is 9 because a number cannot start with 0]
//各个数字不相同
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
if(n==0) return 1;
int res = 10;
int base = 9;
for(int i=2;i<=n&&i<=10;i++){
base = base*(9-i+2);
res += base;
}
return res;
}
};
本文介绍了一种算法,用于计算由n位数构成且每位数字都不相同的数字组合总数。通过递推公式,从1到n位数进行计算,并考虑首位数字不能为0的限制条件。

6996

被折叠的 条评论
为什么被折叠?



