How many times does one of the N numbers from 1 to N appear?

Source: Internet
Author: User
Given a decimal integer N, obtain the number of 1 in all integers from 1 to N. For example, N2, 1, 2, and 1 appear. N12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12. Five 1 s are displayed. The most direct method is to traverse from 1 to N, and add up the number containing 1 in each of them to get the solution.

Given a decimal integer N, calculate the number of "1" in all integers from 1 to N.

For example, N =, 2 shows 1 "1 ".

N =, 12. Five "1" are displayed ".

The most direct method is to traverse from 1 to N, and add up the number of "1" in each of them to get the solution.

public long CountOne3(long n){long i = 0,j = 1;long count = 0;for (i = 0; i <= n; i++){j = i;while (j != 0){if (j % 10 == 1)count++;j = j / 10;}}return count;}

This method is simple and easy to understand, but its problem is efficiency. The time complexity is O (N * lgN). when N is large, it takes a long time.

Let's re-analyze this problem. for any number of digits n, as long as n> = 1, it contains a "1"; n <1, that is, when n = 0, the number of "1" contained is 0. Therefore, we want to use the idea of divide and conquer to reduce the scale of any n-digit into multiple single digits, which makes it easy to solve the problem.

But how can we reduce the scale? After careful analysis, we will find that, the single digit "1" in any n-digit can be divided into two n-1 digits, the number of "1" and the constant C associated with the highest digit. For example, f (12) = f (10-1) + f (12-10) + 3, where 3 indicates the number of digits with the highest bit of 1, which is 10, 11, 12; f (132) = f (100-1) + f (132-100) + 33,33 indicates the number of digits with the highest bit of 1. here it is 100 ~ 132; f (232) = 2 * f (100-1) + f (32) + 100, because 232 is greater than 199, so it includes all the numbers with the highest bit of 1, that is, 100 ~ 199. a total of 100.

In conclusion, we can conclude that the final addition constant C is only related to whether the highest bit n1 is 1. when the highest bit is 1, the constant C is the original number N. after removing the highest bit, the remaining number + 1. when the highest bit is 1, the constant C is 10 bits, where the bit is the number of N-1, for example, if N = 12, bit = 1, N = 232, bit = 2.

Therefore, we can list recursive equations as follows:

If (n1 = 1)
F (n) = f (10bit-1) + f (n-10bit) + n-10bit + 1;
Else
F (n) = n1 * f (10bit-1) + f (n-n1 * 10bit) + 10bit;

The exit condition for recursion is:

If (1
 
  

Based on this, write the following code:

Public long CountOne (long n) {long count = 0; if (n = 0) count = 0; else if (n> 1 & n <10) count = 1; else {long highest = n; // The int bit of the highest digit = 0; while (highest> = 10) {highest = highest/10; bit ++ ;} int weight = (int) Math. pow (10, bit); // indicates the weight of the highest bit, that is, if (highest = 1) {count = CountOne (weight-1) + CountOne (n-weight) + n-weight + 1;} else {count = highest * CountOne (weight-1) + CountOne (n-highest * weight) + weight ;}} return count ;}

This algorithm does not need to traverse 1 ~ N to obtain f (N ). After my tests, the algorithm is much faster than the solution. when the number is within 1010, the algorithm can end in milliseconds. when the number is within 109, the time exceeds 5 minutes. However, this algorithm has a major drawback: when the number exceeds 1010, it will cause Stack Overflow and cannot be calculated.

In addition, I tried for a long time and did not calculate the time complexity of this algorithm. it seems to be O (lg2N). I am not sure, and I hope that experts can give answers.

Solution 2: 1 ~ The number of "1" in N is related to the highest bit. let's look at it from another angle. Given an N, we will analyze 1 ~ The sum of the numbers in N appears at each bit. let's see what determines the sum of the numbers that appear at "1" on each bit.

1-digit condition: it has been analyzed in solution 2. when the value is greater than or equal to 1, there is one. if the value is smaller than 1, No.

2 digits: N = 13. the number of occurrences of 1 in a single digit is 2, which is 1 and 11 respectively. the number of occurrences of 1 in ten digits is 4, which is 10, 11, 12, 13, respectively, so f (N) = 2 + 4. N = 23, the number of occurrences of 1 in a single digit is 3, which is, 21, and the number of occurrences of 1 in ten digits is 10, respectively ~ 19, f (N) = 3 + 10.

As a result, we found that the number of single-digit occurrences of 1 is related not only to single-digit, but also to ten-digit numbers. if the single-digit occurrence of 1 is greater than or equal to 1, then the number of single-digit occurrences of 1 is increased by one; if the single digit is 0, the number of times that a single digit appears is equal to 10 digits. The number of occurrences of one in ten digits is not only related to ten digits, but also to single digits. if ten digits are equal to one, the number of occurrences of one in ten digits is 1 in a single digit, if the number of digits is greater than 1, the number of times that one appears on the number of digits is 10.

Three digits:

N = 123, the number of 1 in a single position is ,..., 91,101,111,121. The number of 10 digits that appear as 1 is 20: 10 ~ 19,110 ~ 119. The number of 1 digits in a hundred bits is ~ 123.

We can continue to analyze the 4-digit and 5-digit data and export the following general situation: suppose N, we want to calculate the number of occurrences of 1 on a hundred bits, which is determined by three parts: the number on a hundred bits, A hundred or more digits and a hundred digits.

If the number on the hundred bits is 0, the number of occurrences of 1 on the hundred bits is determined only by a higher value. for example, 12013, the occurrence of 1 on the hundred bits is 100 ~ 199,1100 ~ 1199,2100 ~ 2199 ,..., 11100 ~ 11199. a total of 1200. The value is equal to or greater than the current number, that is, 12*100.

If the number on the hundred bits is greater than 1, the number of occurrences of 1 on the hundred bits is determined only by a higher level. for example, 12213, the occurrence of 1 on the hundred bits is 100 ~ 199,1100 ~ 1199,2100 ~ 2199 ,..., 11100 ~ 11199,12100 ~ 12199: a total of 1300. Equals to a higher digit plus 1 multiplied by the current digit (12 + 1) x 100.

If the number on the hundred bits is 1, the number of occurrences of 1 on the hundred bits is not only affected by the higher level, but also by the lower level. For example, if 12113 is affected by a high position, 1 may occur: 100 ~ 199,1100 ~ 1199,2100 ~ 2199 ,..., 11100 ~ 11199, a total of 1200, but it is still affected by the low level, the case of 1 is 12100 ~ 12113, a total of 114, equal to the low digit 113 + 1.

Based on the above analysis, write the following code:

public long CountOne2(long n){long count = 0;long i = 1;long current = 0,after = 0,before = 0;while((n / i) != 0){           current = (n / i) % 10;before = n / (i * 10);after = n - (n / i) * i;if (current > 1)count = count + (before + 1) * i;else if (current == 0)count = count + before * i;else if(current == 1)count = count + before * i + after + 1;i = i * 10;}return count;}

The time complexity of this algorithm is only O (lgN), and there is no recursive saving of the consumption and stack overflow issues.

This article is available at http://www.nowamagic.net/librarys/veda/detail/1064.

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.