9/15/2020

[LeetCode] 233. Number of Digit One

Problem : https://leetcode.com/problems/number-of-digit-one/


class Solution:
    def countDigitOne(self, n: int) -> int:
        result, a, b = 0, 1, 1
        
        while n > 0:
            result += (n + 8) // 10 * a
            if n % 10 == 1:
                result += b
                
            b += n % 10 * a
            a *= 10
            
            n //= 10
            
        
        return result

No comments:

Post a Comment