leetcode 357. Count Numbers with Unique Digits

冷不防 2022-09-21 13:22 143阅读 0赞

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])

  1. def fac(m):
  2. if m==0 or m==1:
  3. return 1
  4. return fac(m-1)*m
  5. class Solution(object):
  6. def __init__(self):
  7. self.lookup=[]
  8. self.lookup.append(10)
  9. for i in xrange(2,10):
  10. self.lookup.append(fac(9)/fac(10-i)*(i-1)+fac(9)/fac(9-i))
  11. def countNumbersWithUniqueDigits(self, n):
  12. """
  13. :type n: int
  14. :rtype: int
  15. """
  16. if n>9:
  17. return sum(self.lookup)
  18. if n:
  19. return sum(self.lookup[0:n])
  20. return 1

发表评论

表情:
评论列表 (有 0 条评论,143人围观)

还没有评论,来说两句吧...

相关阅读