Offer Penalty for prove safety (31): 1 integer number appears (the number of occurrences n is an integer from 1 to 1)

Offer Penalty for prove safety (31): 1 integer number appears (the number of occurrences n is an integer from 1 to 1)

Search micro-channel public number: 'AI-ming3526' or 'this small computer vision' for more algorithms, machine learning, dry
CSDN: https://blog.csdn.net/baidu_31657889/
GitHub: https://github.com/ aimi-cn / AILearners

First, the primer

This series is my brush "to prove safety Offer" brush off the cattle in question notes online, it aims to enhance the ability under its own algorithm.
View the complete algorithm to prove safety issues resolved Offer Click CSDN and github link:
prove safety Offer complete analytical exercises CSDN address
github address

Second, the title

Calculated the number of integer of 1 to 13 1 arise, and calculates the number of times an integer of 1 to 1 in 1300 appearing? To this end he especially counted about 1 to 13 contains the digits 1 has 1,10,11,12,13 were therefore appears five 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.

1, ideas

Method 1: The most intuitive, for each integer of 1 ~ n, respectively, is determined in a number n, in particular, see "the offer to prove safety." Time complexity of this method is O (N * logN), when N is large, the general time out.

Method 2: This category of problems, if not solved intuitive, then usually conducted to find the law, transformed into a mathematical problem. This question is at the "Beauty of Programming" has a more detailed description, the following examples in conjunction with a specific analysis:

Prior to the analysis, we first need to know a rule:

  • From 1-10, in their single digits, the number 1 appears 1 times.
  • From 1 to 100, in their ten digits, the digital 1 appears 10 times.
  • From 1-1000, in their hundreds digit, the number 1 appears 100 times.
    And so on, from 1 to 10i, in the second place from the right thereof, the number 1 appears 10 ^ (i - 1) times.

For n = 2134, to find this number from 1 to 2134 2134 1 all numbers. We can be bit-wise analysis of 2134:

(1) in bits, from 1 to 2130, contains 10 213, 213 so the number 1 appears twice, the remaining number of digits only 2131,2132,2133,2134 2131 resin containing a word, left not included under. Therefore, the total number of digits in the number 1 is 213 + 1 = 214.

(2) in ten, from 1 to 2100, contains 21 100, the digital 1 appears 21 * 10 = 210 times, and the remaining ten numbers from 2101 ~ 2134, 2110 ~ 2119 which is only 10 digits in digital bit is 1, the total number of digits in the ten 1 is 210 + 10 = 220.

(3) at the one hundred, from 1 to 2000, it contains two 1000, the digital 1 appears 2 * 100 = 200, the remaining number from 2001 to 2134, from 2100 to 2134 only numbers in the 35 one hundred number is 1, the total number of the hundreds digit numbers 1 to 200 + 35 = 235.

(4) on the one thousand, it contains 0 10000, so the number 1 appeared in 1000 * 0 = 0, one thousand numbers the rest of the numbers 1000 to 1999, only the 1000 figures is 1, so one thousand figures on the total number of 1 bits is 1000.

Therefore, from 1 to 2134 in the n numbers, the total number of the number that appears is 214 + 220 + 235 + 1000 = 1669.

What steps summarized above, you can get such a rule:

For the number n, calculating its first i (i from 1 starts, counting from the right) containing a number of digits of the number of bits:

Assumptions made on the i-th bit of x, then

1. If x> 1 then the number 1 is included in the i-th digit of the upper digit :( + 1) * 10 ^ (i-1) (which is the higher digit bit remains from i + 1 to the maximum number of bits consisting of numbers)

2. If x <1, then the number 1 is included in the i-th digit of the upper digit :() * 10 ^ (i-1)

3. If x == 1, then the number of bits comprising the i-th :( 1 is higher digit) * 10 ^ (i-1) + (lower digit 1) (wherein when the lower digit from the i - 1 digits until the first digit of numbers)

2, programming

python

Code implementation:

# -*- coding:utf-8 -*-
class Solution:
    def NumberOf1Between1AndN_Solution(self, n):
        # write code here
        if n < 1:
            return 0
        # mult位数 sumTime出现1的次数和
        mult, sumTimes = 1, 0
        while n//mult:
            div, mod = divmod(n, mult*10)
            curNum, curMod = divmod(mod, mult)
            if curNum > 1:
                sumTimes += div*mult + mult
            elif curNum == 1:
                sumTimes += div*mult + curMod + 1
            else:
                sumTimes += div*mult
            mult = mult*10
        return sumTimes

AIMI-CN AI learning exchange group [1015286623] for more information on AI

Sharing technology, fun in life: our number of public computer vision this small push "AI" series News articles per week, welcome your interest!

Guess you like

Origin www.cnblogs.com/aimi-cn/p/11510770.html