Old Wei wins the offer to take you to learn --- brush (integer number 31. 1 appears) title series

31. The integer number 1 appears

problem:

The number 1 appears determined integer 1-13, and calculates the number of integer 100-1300 in 1 appearing? To this end he especially counted 1-13 contains the digits 1 has 1,10,11,12,13 therefore appear a total of six times, but for the problem behind him Meizhe. ACMer hope you will help him, and the problem is more generalized, the number of non-negative integer in the range 1 appears (the number of occurrences from 1 to n. 1) can quickly find any.

solve:

Similar such problems, we can summarize by acquiring related things.

First, you can first category:

Bit
we know in the single digits, 1 will occur once every 10, 11, 21, etc. For example, we found that 10 as a ladder, then each has a complete ladder inside a 1, for example figures 22 , according to 10 points at intervals of three steps, in full ladder 0-9,10-19 have a 1, but there is an incomplete ladder after 19, we need to judge this ladder will not appear in 1 easy to infer know, if the last part of this exposed to less than 1, it is impossible 1 (summarized for doing this is also true of other figures).

We can generalize on the number of bits 1 appears as follows:

n/10 * 1+(n%10!=0 ? 1 : 0)

Ten
now say ten digits, case 1 appears on the ten digits should be 10-19, still in use ladder theoretical single-digit time, we know that this set of numbers 10-19, 100 occur every time our ladder is 100, 317 such as digital analysis a step ladder 0-99,100-199,200-299 complete three sections, each section of the ladder which will appear 10 times 1 (from 10-19), the final analysis be revealed During that incomplete ladder. We considered exposed if the number is greater than 19, then the direct count 10 1 on the line, because there will be 10-19; if less than 10, it certainly does not appear Tens of 1; if between 10-19 we calculate the result should be a k - 10 + 1. For example, we analyzed 300-317,17 numbers, number 1 should appear 17-10 + 1 = 8.

It can now be summarized: ten on the number 1 appears as follows:

设k = n % 100,即为不完整阶梯段的数字
归纳式为:(n / 100) * 10 + (if(k > 19) 10 else if(k < 10) 0 else k - 10 + 1)

One hundred
now say one hundred 1, we know that in one hundred, one hundred 1 100-199 will appear, appeared a total of 100 times, stepped intervals 1000,100-199 this set of numbers, 1000 will appear every time. We assume that this number is 2139. Consistent with the above idea, to count the number of steps in the step 1 * complete number appears in the one hundred, i.e., n / 1000 * 100 number obtained in the step 1 of the first two, then the count then escaping portion 139, follow the above-described Thought, the number of incomplete ladder k199, one hundred to give 100 1,100 <= k <= 199 is obtained k - 100 + 1 percentage bits.

Then continue summarized the number 1 appears on one hundred:

设k = n % 1000
归纳式为:(n / 1000) * 100 + (if(k >199) 100 else if(k < 100) 0 else k - 100 + 1)

Back and so on ...

Recalling bit again
our formula to a single-digit number is also included to get on an inductive formula

k = n % 10
个位数上1的个数为:n / 10 * 1 + (if(k > 1) 1 else if(k < 1) 0 else k - 1 + 1)

perfect! Inductive seems to have a very structured. Summed to a more abstract, is provided to calculate the number of bits i 1 where, i = 1 indicates that the calculation of the digit number of 1, 10 denotes a ten-digit number is calculated, and the like.

k = n % (i * 10)
count(i) = (n / (i * 10)) * i + (if(k > i * 2 - 1) i else if(k < i) 0 else k - i + 1)

Well, so that the n-th power from the inductive 10-10 is completed.

sum1 = sum(count(i)),i = Math.pow(10, j), 0<=j<=log10(n)

But there is a place worthy of our attention, is the simplicity of the code point of view, there are more ifelse is not very good, can further simplify it? We can simplify the latter half in such a way, we do not calculate i * 2 - 1, we only need to ensure k - i + 1 in the [0, i] on the line interval, can be written after the last half of this

min(max((n mod (i*10))−i+1,0),i)

python code:

# -*- coding:utf-8 -*-
class Solution:
    def NumberOf1Between1AndN_Solution(self, n):
        # write code here
        i = 10 ** (len(str(n))-1)
        count = 0
        while i != 0:
            k = n % (10 * i)   ##计算差余
            count += n // (i * 10) * i   ###计算阶梯数
            if k > i * 2 - 1:
                count += i
            elif k < i:
                count += 0
            elif i <= k <= i * 2 - 1:
                count += k - i + 1

            i = i // 10
        return count

Of course, there is a violent method:

class Solution:
    def NumberOf1Between1AndN_Solution(self, n):
        # write code here
        ans = 0
        for i in range(1,n+1):
            ans += str(i).count('1')
        return ans

Published 160 original articles · won praise 30 · views 70000 +

Guess you like

Origin blog.csdn.net/yixieling4397/article/details/104976134