357. Count Numbers with Unique Digits
357. Count Numbers with Unique Digitsclass Solution(object):
def countNumbersWithUniqueDigits(self, n):
"""
:type n: int
:rtype: int
"""
res = 0
if n == 0:
return 1
while n > 0:
res += self.helper(n)
n -= 1
return res
def helper(self, n):
if n == 1:
return 10
res = 9*9
n -= 2
temp = 8
while n > 0:
n -= 1
res *= temp
temp -= 1
return res
我写的代码真是没啥美感,来份大佬的看下吧。
class Solution(object):
def countNumbersWithUniqueDigits(self, n):
"""
:type n: int
:rtype: int
"""
if n == 0:
return 1
g, h = 10, 9
for i in range(n-1):
g += 9*h
h *= (8-i)
return(g)
网友评论