43 1~n 整数中1出现的次数

class Solution():
    def count1(self,n):
        base = 1
        count = 0
        r = n
        while r > 0:
            weight = r%10
            r = int(r/10)
            count += r*base
            if weight == 1:
                count+=(n%base)+1
            elif weight >1:
                count+=base
            base*=10

        return count








s = Solution()
print(s.count1(534))

你可能感兴趣的:(43 1~n 整数中1出现的次数)