# way 1
# -*- coding:utf-8 -*-
class Solution:
def NumberOf1Between1AndN_Solution(self, n):
# write code here
# -*- coding:utf-8 -*-
precise = 1
highValue = 1
midValue = 1
lowValue = 1
count = 0
numSum = 0
while highValue != 0:
highValue = n // (precise * 10)
midValue = (n // precise) % 10
lowValue = n % precise
precise = precise * 10
if midValue == 0:
num = (highValue -1 + 1) * pow(10, count)
elif midValue > 1:
num = (highValue + 1) * pow(10, count)
else:
num = highValue * pow(10, count) + (lowValue + 1)
numSum += num
count += 1
return numSum
# way 2
class Solution:
def NumberOf1Between1AndN_Solution(self, n):
# write code here
count = 0
for i in range(1,n+1):
a = len(str(i))
while(a):
if (i%10 == 1):
count += 1
i = i//10
else:
i =i//10
a -= 1
return count