Title: Enter an integer n to find the number of occurrences of 1 in decimal notation from 1 to n integers. 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.
Method One: Do not consider the time efficiency of the solution, rely on it to get an offer a little difficult:
If you encounter this problem during an interview, most of the candidates can think of the most intuitive method, which is to accumulate 1 to n each integer 1 occurrences. We suspect that every single digit is 1 if the integer is judged by the remainder of 10. If this number is greater than 10, divide by 10 to determine if the digit is 1. Based on this idea, we write the following code:
/** * 1 occurrences from 1 to n integers */package swordforoffer;/** * @author Jinshuangqi * * August 8, 2015 */public class E32numberof1 {public in t Numberof1betweenandn (int n) {int number = 0;for (int i = 1;i<= n;i++) {number+=numberof1 (i);} return number;} public int numberOf1 (int n) {int number =0;while (n!=0) {if (n%10 = = 1) number++;n = N/10;} return number;} public static void Main (string[] args) {int n = 12; E32NUMBEROF1 test = new E32numberof1 (); System.out.println (TEST.NUMBEROF1BETWEENANDN (n));}}
From the above thinking, we have to do the starting and the remainder of each number to make the number of 1 occurrences. If the input number N,n has O (logn) bit, we need to determine whether each bit is 1, then its time complexity is O (N*LOGN). When the input n is very large, a large number of computations are required and the computational efficiency is not high. The interviewer will not be satisfied with the algorithm.
Method Two: From the digital law to improve the time efficiency of the solution, can let the interviewer refreshing
If you want to not calculate the number of 1 per digit, then you can only find 1 in the number of the law. In order to find the pattern, we might as well use a slightly larger number such as 21345 as an example to analyze. We divide all numbers from 1 to 21345 into two segments, one from 1 to 1345 and the other from 1346 to 21345.
Let's first look at the number of occurrences from 1346 to 21345 in 1. 1 appears in two cases. First analyze the situation where 1 appears at the highest bit. Of the numbers from 1346 to 21345, 1 appeared in the 10000--19999 of the 10,000 digits, with a total of 10,000.
It is worth noting that not all of the 5-digit number in the number of occurrences is 10,000, for the million digit is 1 of the number such as the input 12345,1 appear in the 10000--12345, the number of occurrences is not 10000, but 2,346 times, That is, the number remaining after the highest digit is removed plus 1 (i.e. 2345+1= 2346)
The next step is to analyze 1 of the four-bit numbers that appear outside the highest bit. For example, in the 20,000 digits of the 1346--21345, the number of 4 digits in the second 1 appears 2000 times. Since the highest bit is 2, we suspect that the number of the second 4 bits appearing in the 1346--21345 20,000 numbers is 2000 times. Since the highest bit is 2, we can then divide the 1346--21345 into two segments, 1346--11345 and 11346--21345. Each of the remaining four digits, select one of them is 1, the remaining three bits can be arbitrarily selected in the 10 numbers of 0--9, Therefore, the total number of occurrences according to the permutation combination principle is 2*10 (2) = 2000 times.
As for the number of occurrences from 1 to 1345 in 1, we can use recursion to obtain it. This is why we divide the 1--21345 into 1--1345 and 1346--21345 two. Because the highest bit of 21345 is removed and programmed to 1345, it is convenient for us to adopt the idea of recursion.
The implementation code is no longer written.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sword Point of Offer (Java Edition): Number of occurrences from 1 to n integers 1