题目描述:给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10^n 。
题目链接:357. 计算各个位数不同的数字个数
乘法原理。
class Solution {
public:
int countNumbersWithUniqueDigits(int n) {
if (n==0) return 1;
if (n==1) return 10;
long long ans=9;
long long now=9;
int cnt=2;
long long tip=10;
while (cnt<=n){
ans*=now;
if (ans==0) return 0;
tip+=ans;
now--;
cnt++;
}
return tip;
}
};