剑指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

 

 

 

posted @ 2018-10-26 16:16  findtruth123  阅读(606)  评论(1编辑  收藏  举报