<p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def countDigitOne(self, n: int) -> int:
            base = {
                0: 0,
                1: 1,
                2: 20,
                3: 300,
                4: 4000,
                5: 50000,
                6: 600000,
                7: 7000000,
                8: 80000000,
                9: 900000000,
                10: 1000000000
            }

            ans = 0
            i = 0  
            number = n
            lower_part = 0 

            while number > 0:
                digit = number % 10
                number //= 10
                
                if digit > 1:
                    ans += digit * base[i] + 10 ** i
                elif digit == 1:
                    ans += digit * base[i] + lower_part + 1
                else:
                    ans += digit * base[i]

                lower_part = n % (10 ** (i + 1))
                i += 1 

            return ans

16600