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://leetcode.com/problems/count-numbers-with-uniq...
- Given a non-negative integer n, count all numbers with un...
- Problem Given a non-negative integer n, count all numbers...
- 问题: Given a non-negative integer n, count all numbers wit...
- My code: reference:https://discuss.leetcode.com/topic/480...