LeetCode 1067. [Python] Digit Count in Range
原题链接
困难
作者:
徐辰潇
,
2021-03-03 02:32:04
,
所有人可见
,
阅读 577
class Solution:
def digitsCount(self, d: int, low: int, high: int) -> int:
#Same as problem 233
def helper(d, n):
S = str(n)[::-1]
res = 0
if d != 0:
for i in range(len(S)):
left = n // (10**(i+1))
res = res + left*(10**i)
if int(S[i]) > d:
res = res + 10**i
elif int(S[i]) == d:
right = n % (10**i) + 1
res = res + right
else:
for i in range(len(S)):
left = n // (10**(i+1))
res = res + (left-1)*(10**i)
if int(S[i]) > d:
res = res + 10**i
elif int(S[i]) == d:
right = n % (10**i) + 1
res = res + right
return res
return helper(d, high) - helper(d, low-1)