forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1067.py
24 lines (23 loc) · 784 Bytes
/
1067.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
def digitsCount(self, d: int, low: int, high: int) -> int:
def digits(k, n):
base, cnt = 1, 0
while n // base:
cur = n//base%10
l = n%base
r = n//(base*10)
if cur == k:
if k == 0:
cnt += (r - 1) * base + l + 1
else:
cnt += 1 + base * r + l
elif cur < k:
cnt += base * r
else:
if k == 0:
cnt += base * r
else:
cnt += base * (r + 1)
base *= 10
return cnt
return digits(d, high) - digits(d, low-1)