Problem Description
Given a decimal integer n, the number of "1" occurrences in all integers from 1 to n is calculated.
For example: 1 "1" appeared in n=2.
n=12 when 1,2,3,4,5,6,7,8,9,10,11,12. There were 5 "1".
Method one brute force solution
The most direct way is to start from 1 to N, each of which contains the number of "1" added together, the problem is solved.
The code is as follows:
|
public long CountOnes( long n){ |
|
public static void main(String args[]){ |
|
CountOnes c = new CountOnes(); |
|
System.out.println(c.CountOnes( 12 )); |
|
System.out.println(c.CountOnes( 120 )); |
The time complexity of the algorithm is O (N*LGN)
Solution Two
1-digit situation:
In the solution two has been analyzed, greater than or equal to 1, there are 1, less than 1 is not.
2-digit situation:
N=13, the number of single-digit occurrences of 1 is 2, respectively 1 and 11, 10 digits 1, respectively, is 10,11,12,13, so f (N) = 2+4.
N=23, the number of single-digit occurrences of 1 is 3, respectively, 1,11,21, 10 digits 1 of the number of 10, respectively, 10~19,f (N) =3+10.
Thus we found that the number of single-digit occurrences of 1 is not only related to single digit, and 10 digits are also related, if single digit is greater than or equal to 1, the number of single digit 1 is 10 digits plus 1, if single digit is 0, the number of single digit 1 is equal to 10 digit number. The number of 10-digit occurrences of 1 is not only related to the 10-digit number, but also to the single-digit: If the 10-digit number equals 1, 10 is the number of digits on the 1-digit number plus 1, and if the 10-digit is greater than 1, 10 is 1 on the 10-digit number.
3-digit situation:
N=123
The number of single digit occurrences is 13:1,11,21,...,91,101,111,121.1
The number of 10-bit occurrences of 1 is 20:10~19,110~119
The number of hundreds of 1 appears to be 24:100~123
We can continue to analyze 4-digit, 5-digit numbers and derive the following general situation:
Suppose N, we are going to calculate the number of occurrences of 1 on the hundred, will be determined by three parts: the number on the hundred, the number above the hundred, the number of the hundred.
If the number on the hundred is 0, then the number of 1 on the hundred is determined only by a higher level, such as 12013, 1 of the case is 100~199,1100~1199,2100~2199,...,11100~11199, a total of 1200. equals the higher number multiplied by the current number of digits, which is 12 * 100.
If the number on the hundred is greater than 1, then the number of occurrences of 1 on the hundred is determined only by higher levels, such as 12213, 1 of the total is 1300 100~199,1100~1199,2100~2199,...,11100~11199,12100~12199. equals the higher number plus 1 times the current number of digits, i.e. (12 + 1) *100.
If the number on the hundred is 1, the number of 1 on the hundred will be affected not only by higher levels but also by low levels. For example 12113, affected by high Level 1 of the situation: 100~199,1100~1199,2100~2199,...,11100~11199, a total of 1200, but it is also affected by the low, 1 of the situation is 12100~12113, a total of 114, equals Low digit 113+1.
To synthesize the above analysis, write the following code:
01 |
public long CountOne2( long n) { |
04 |
long current = 0 , after = 0 , before = 0 ; |
06 |
current = (n / i) % 10 ; |
07 |
before = n / (i * 10 ); |
08 |
after = n - (n / i) * i; |
11 |
count = count + (before + 1 ) * i; |
13 |
count = count + before * i; |
15 |
count = count + before * i + after + 1 ; |
Count 1 between 0 and N