Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])
找出最多n位整数中没有重复位的整数的个数
1:0,1,2,3,4,5,6,7,8,9——10
2:99——因为在选定第一位后第二位还有9种选择,第一位不能选0也是9种
3:998——前两位选定就只剩8种了
4:9987
...
10:998765432*1
11:0
...
所以其实你可以把所有数写到一个数组里哈哈
var countNumbersWithUniqueDigits = function(n) {
if (n === 0) return 1;
var res = 10;
var uniqueDigits = 9;
var availableNumber = 9;
while (n-- > 1 && availableNumber > 0) {
uniqueDigits = uniqueDigits * availableNumber;
res += uniqueDigits;
availableNumber--;
}
return res;
};