剑指offer python版 从1到n整数中1出现的次数


def NumberOf1Between1AndN_Solution(self, n):
    mult, sumTimes = 1, 0 
    while n//mult > 0:
        high, mod = divmod(n, mult*10) 
        curNum, low = divmod(mod, mult) 
        if curNum > 1:
            sumTimes += high*mult + mult 
        elif curNum == 1:
            sumTimes += high*mult + low + 1 
        else: 
            sumTimes += high*mult 
        mult = mult*10 
        
    return sumTimes


 
原文地址:https://www.cnblogs.com/xzm123/p/9857049.html