Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.
Example:
Input: 13
Output: 6 
Explanation: Digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.
class Solution:
     def countDigitOne(self, n):
         """
         :type n: int
         :rtype: int
         """
         if n <= 0:
             return 0
         q, x, ans = n, 1, 0
         while q > 0:
             digit = q % 10
             q = q // 10
             ans += q * x
             if digit == 1:
                 ans += n % x + 1
             elif digit > 1:
                 ans += x
             x *= 10
         return ans
class Solution:
     def countDigitOne(self, n):
         """
         :type n: int
         :rtype: int
         """
         if n <= 0:
             return 0
         q, x, ans = n, 1, 0
         while q > 0:
             digit = q % 10
             q = q // 10
             ans += q * x
             if digit == 1:
                 ans += n % x + 1
             elif digit > 1:
                 ans += x
             x *= 10
         return ans