The sword refers to the offer question (Java version): Number of occurrences of 1 from 1 to n integers __ sword refers to offer (Java edition)

Source: Internet
Author: User

Title: Enter an integer n to find the number of occurrences of 1 in the decimal representation of 1 to n integers. For example, the input 12, from 1 to 12 of these integers containing 1 of the number has 1,10,11, and 12, 11 appeared in total 5 times.

Method One: does not consider the time efficiency the solution, relies on it to obtain the offer is somewhat difficult:

If you encounter this problem in an interview, most of the candidates can think of the most intuitive way, that is, add 1 to N of the number of each integer 1 occurs. We're suspicious. It is not 1 to judge the number of integers by the remainder of 10. If this number is greater than 10, divide by 10 and then judge if single-digit digits are 1. Based on this idea, we write the following code:

/**
 * The number of 1 occurrences from 1 to n integers * *
package swordforoffer;

/**
 * @author Jinshuangqi
 * *
 * August 8, 2015/public
class E32numberof1 {public
	int 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 idea, we have to do the starting and residual operations for each number to find out the number of 1 occurrences of the number. If the input number N,n has an O (logn) bit, we need to determine if each bit is 1, then its time complexity is O (N*LOGN). When the input n is very large, it requires a lot of calculation, the operation efficiency is not high. The interviewer will not be satisfied with this algorithm.

Method Two: Starting from the digital law to improve the time efficiency of the solution, can make the interviewer refreshing

If you don't want to calculate the number of 1 of each number, you can only look for 1 of the rules that appear in numbers. In order to find the law, we might as well use a slightly larger number like 21345 as an example to analyze. We divide all the numbers from 1 to 21345 into two segments, one from 1 to 1345 and the other from 1346 to 21345.

Let's look at the number of occurrences of 1 from 1346 through 21345. 1 appears in two situations. First, we analyze the situation where 1 appears at the highest level. From 1346 to 21345 of the figures, 1 appeared in the 10000--19999 of the 10,000 digits, and a total of 10,000 appeared.

It is noteworthy that not all of the 5 digits in the number of occurrences of 10,000, for the million is 1 of the number, such as input 12345,1 appear in 10000--12345 million, the number of times is not 10000, but 2,346 times, That is, remove the highest digit number and then add 1 (ie 2345+1= 2346)

Next, analyze the case of 1 other four digits that appear outside the highest digits. For example, the number of the 20,000 digits in the latter 4 digits in 1346--21345 is 2000 times. Since the highest is 2, we suspect that the number of 1 occurrences in the latter 4 digits of the 1346--21345 20,000 digits is 2000 times. Since the highest bit is 2, we can divide the 1346--21345 into two sections, 1346--11345 and 11346--21345. Of the remaining four digits in each paragraph, select one of them is 1, and the remaining three can be selected in 0--9 10 digits, Therefore, the total number of occurrences of 2*10 (2) = 2000 times according to the permutation and combination principle.

As for the number of occurrences from 1 to 1345 1, we can use recursion to find out. This is why we divide the 1--21345 into 1--1345 and 1346--21345 two paragraphs. Because the top of the 21345 to remove the programming of 1345, so that we can use the idea of recursion.

The implementation code is no longer written



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.