classSolution:defcountDigitOne(self,n:int) ->int:# 初始化一些变量 digit, ret =1,0 hi, cur, lo = n //10, n %10,0while hi !=0or cur !=0:if cur ==0: ret += hi * digitelif cur ==1: ret += hi * digit + lo +1else: ret += (hi +1) * digit lo += cur * digit cur = hi %10 hi //=10 digit *=10return ret