题目:给定一个非负整数n,计算各位数字都不同的数字x的个数
法:n=0时,return 1;n=1时,x=0~9都是不同的,return 10,其中10-9+1
n=2时,只有一位但不同的有10个,有两位且每位数字都不同的有9*9=81,所以总体return 81+10=91
n=3时,有三位但每位数字都不同的有9*9*8=648,再加上n=2时的返回结果91,总体return 648+91=739
所以当n=k时,有k位但每位数字都不同的有9*9*8*.....*(10-k+1)。但当k>=10之后,不能存在有十位但每位数字都不同的数字,所以会先在k与9之间取最小
def countNumbersWithUniqueDigits(self, n):
"""
:type n: int
:rtype: int
"""
res=1 #这里初始化为1不为0,因为n=0时return 1
for i in range(min(9,n),0,-1):
tmp=1
for j in range (i):
if j==0:tmp*=9
else:tmp*=10-j
res+=tmp
#print(tmp)
return res