统计各位数字都不同的数字个数

🎯 问题描述(来源于LeetCode)

1
给你一个整数 n ,统计并返回各位数字都不同的数字 x 的个数,其中 0 <= x < 10^n 。

💻 代码实现

1
2
3
4
5
6
7
8
9
10
11
class Solution:
def countNumbersWithUniqueDigits(self, n: int) -> int:
if n == 0:
return 1
total = 1
for k in range(1, n+1):
count = 9
for i in range(9, 9 - (k-1), -1):
count *= i
total += count
return total

📊 性能分析

提交结果

  • 运行时间:0ms击败100.00%
  • 内存消耗:17.27MB击败98.66%

复杂度验证

  • 时间复杂度:O(N2)O(N^2)
  • 空间复杂度:O(1)O(1)