An algorithm question-the number of occurrences of 1 in integers from 1 to n

Source: Internet
Author: User

1. Title Description

Enter an integer n to find the number of occurrences of the decimal representation of the n integers from 1 to n. 1. For example, enter 12, from 1 to 12 these integers contain 1 of the numbers have 1,10,11 and 12, 11 have appeared 5 times.

2. Source of the topic

First seen in the "sword point of Offer" on the 2nd edition, face question 32. Leetcode and cattle online have this problem.

3. The purpose of this article

Look at the "sword Point offer" on the solution, I think it is not good:

    1. This explanation is a little unclear, and it's hard to understand without a diagram.
    2. From the book given in the implementation of the view, seems a little messy.

In this blog, I will give a solution to this problem, including the complete problem-solving ideas, complete code, time complexity analysis.

4. Solving Ideas

Consider each bit of the decimal of n as a separate discussion, with the value of each bit recorded as weight.

1) digit

From 1 to N, each additional 1,weight will be added 1, when weight is added to 9 o'clock, plus 1 will return to 0 to start again. So how many times does the weight from 0-9 of this cycle occur? It depends on how high the n is, look at the graph:


Taking 534 as an example, in the process of growing from 1 to N, 534 of the bits changed from 0-9 to 53 times, recorded as round. In each round of change, 1 appeared once in a single digit, so there were 53 times.
Then look at the value of weight. Weight is 4, greater than 0, indicating that the 54th round change is from 0-4, 1 has appeared 1 times. We remember that 1 occurrences count, so:

Count = Round+1 = 53 + 1 = 54


If the weight is 0 (n=530) at this point, the 54th round to 0 stops, then:

Count = Round = 53

2) 10-bit

For 10-bit, the number of occurrences of the 0-9-cycle period is the same as the single-digit statistic, see figure:


The difference is: from 1 to n, each increase of 10, 10-bit weight will be increased by 1, so, a 0-9-cycle period, 1 will appear 10 times. That is rount*10.
Then look at the value of weight. When this time weight is 3, greater than 1, indicating that the 6th round appears 10 times 1, then:

Count = round*10+10 = 5*10+10 = 60


If the value of weight at this time is equal to 0 (n=504), the 6th round to 0 stops, so:

Count = round*10+10 = 5*10 = 50


if the value of weight at this time equals 1 (n=514), how many times does 1 appear in the 6th round? Obviously, this is related to the single-digit value, the digit number is K, the 6th round of 1 appears k+1 (0-K). we record the single digit as former, then:

Count = Round*10+former +1= 5*10+4 = 55

3) More high

The more high-level calculation is in fact consistent with the 10-bit, no longer elaborated.

4) Summary

Divide the bits of n into two categories: bits and others.
For the single digit:

    • If the digit is greater than 0, the 1 occurrences areround*1+1
    • If the digit is equal to 0, 1 appears as the number ofround*1

For other bits, the weight of each bit is base, the bit value is weight, the number before the bit is former, for example


The

    • If weight is 0, then 1 occurrences areround*base
    • If weight is 1, then 1 occurrences areround*base+former+1
    • If weight is greater than 1, 1 occurrences arerount*base+base

Like what:

    • 534 = (digit 1 occurrences) + (10 bit 1 occurrences) + (hundred 1 occurrences) = (53*1+1) + (5*10+10) + (0*100+100) = 214
    • 530 = (53*1) + (5*10+10) + (0*100+100) = 213
    • 504 = (50*1+1) + (5*10) + (0*100+100) = 201
    • 514 = (51*1+1) + (5*10+4+1) + (0*100+100) = 207
    • 10 = (1*1) + (0*10+0+1) = 2
5. Complete code
1 intNumberof1between1andn_solution (intN)2     {3         if(n<1)4             return 0;5         intCount =0;6         int Base=1;7         intRound =N;8          while(round>0)9         {Ten             intWeight = round%Ten; OneRound/=Ten; ACount + = round*Base; -             if(weight==1) -count+= (n%Base)+1; the             Else if(weight>1) -count+=Base; -             Base*=Ten; -         } +     returncount; -}
6. Time complexity Analysis

From the analysis of ideas or code can be seen, while the number of times is the number of N, Logn (base 10), and the operation of the loop in the body is limited, so the time complexity of O (Logn).

Reference: http://blog.csdn.net/yi_afly/article/details/52012593

An algorithm question-the number of occurrences of 1 in integers from 1 to n

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.