package main
import "fmt"
func countNumbersWithUniqueDigits(n int) int {
if (n == 0) {
return 1
}
var ret = 10
var k = 9
for i := 2; i <= n && i <= 10; i++ {
k = k * (9 - i + 2)
ret += k
}
return ret
}
// ----------------------
func main() {
fmt.Println(countNumbersWithUniqueDigits(9))
}
问题
https://leetcode.com/problems/count-numbers-with-unique-digits/description/
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的增加,是有规律的
解答