Count Numbers with Unique Digits
作者:
敲一手烂代码 | 来源:发表于
2018-01-31 14:42 被阅读16次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])
func countNumbersWithUniqueDigits(_ n: Int) -> Int {
if n == 0 {
return 1
}
var res = 10
var count = n
var perCount = 9
var idx = 9
while count > 1 && idx > 0 {
perCount *= idx
count -= 1
idx -= 1
res += perCount
}
return res
}
本文标题:Count Numbers with Unique Digits
本文链接:https://www.haomeiwen.com/subject/brakzxtx.html
网友评论