website/content/ChapterFour/0300~0399/0357.Count-Numbers-with-Unique-Digits.md
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
Input: 2
Output: 91
Explanation: 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,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10^n 。
package leetcode
// 暴力打表法
func countNumbersWithUniqueDigits1(n int) int {
res := []int{1, 10, 91, 739, 5275, 32491, 168571, 712891, 2345851, 5611771, 8877691}
if n >= 10 {
return res[10]
}
return res[n]
}
// 打表方法
func countNumbersWithUniqueDigits(n int) int {
if n == 0 {
return 1
}
res, uniqueDigits, availableNumber := 10, 9, 9
for n > 1 && availableNumber > 0 {
uniqueDigits = uniqueDigits * availableNumber
res += uniqueDigits
availableNumber--
n--
}
return res
}