解题思路:自己的思路太复杂了,参考的题解的思路
代码如下:
class Solution:
def countDigitOne(self, n: int) -> int:
low = 0
res = 0
digit = 1
while(n):
cur = n % 10
high = n // 10
if cur == 0:
res += high * digit
elif cur == 1:
res += high * digit + low + 1
else:
res += (high + 1) * digit
low += cur * digit
digit *= 10
n //= 10
return res