Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.
For example:
Given n = 13,
Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.
class Solution(object):
def countDigitOne(self, n):
"""
:type n: int
:rtype: int
"""
TMP=n
count=0
ret=0
while(TMP>0):
num=TMP%10
ans=0
if num==1:
ans+=n%(10**(count+1))-10**(count)+1
elif num>1:
ans+=10**(count)
cnt=count-1
flag=0
while(cnt>=0):
ans+=(10**flag)*(10**cnt)*num
flag+=1;cnt-=1
TMP=TMP/10
count=count+1
ret=ret+ans
return ret