Number of times that Microsoft's 100 Question 30 appears in an integer from 1 to n, an integer

Source: Internet
Author: User

Number of times that Microsoft's 100 Question 30 appears in an integer from 1 to n, an integer

30. Number of times 1 appears in an integer from 1 to n
Question: enter an integer n to calculate the number of times 1 appears in the decimal representation of the n integers from 1 to n.
For example, input 12, integers from 1 to 12 contain numbers 1, 10, 11, and 12, and 1 appear 5 times in total.
Analysis: This is a widely used google interview question.

 

Ideas:

The first reaction is to traverse the N numbers, and then calculate the number of times of 1 on each bit by using the 10 remainder for each positive number n. Similarly, the number of times that 1 appears for each number is calculated and accumulated, however, the time complexity of this method is o (n). It takes too long when the n value is very high.

The second idea is whether we can find the number of times of all 1 on n through mathematical methods (I can't think of it myself. I first searched the internet, and finally found that I am the most clear about programming, here I try to summarize it in my own language)

The first is an important rule: for a specific number n, the number of occurrences of 1 from 1 to n must be equal to the number of occurrences of 1 in a single digit + the number of occurrences of 1 in ten digits + the number of occurrences of 1 in a hundred digits + the number of occurrences of 1 in a thousand digits... For example

F (3) = 1: Only one digit appears

F (13) = 1 in a single position, 1 in 11, 10 in 10 in 12 13 = 2 + 4

F (23) = number of single-digit 11 11 21, 10 11 12 13... 19 = 3 + 10

F (33) = number of digits in a single position 1 11 21 31, 10 11 12 13 on 10... 19 = 4 + 10

F (93) = the number of single-digit numbers 1 11... 91, 10 11 12 13... 19 = 10 + 10

F (203) = number of digits 1 11 21... 91,101,111,121... 191,201 = 21, 10, 11, 12... 19,110... 119 = 20, 100,101,199 = 100 =

F (12013) = the number of 1 in a hundred bits: 100-2199-100-1200-100,... 9100-9199....11100-11199, total 12 * =, that is, high digits * Current digits

F (12113) = the number of 1 digits in a hundred bits is the same as that in a high-digit 12 * Current-digit 100 = 1200, plus 12100-12113 = 13 + 1, that is, the low-digit low + 1, the total value is high * 100 + low + 1.

Through the above example, we can find that the frequency of occurrence of the first hundred bits is affected by three factors:

1. if the number is greater than 12013 bits, we call it high. For example, for 100, the value of high is 12, and the number of occurrences of 1 bits on 199,110 bits is affected by the high value:-0-1199....9100-9199, 10100-10199,11100-11199, because of the impact of high, it is 12*100 = high * 100

2. for a hundred-bit cur, if the number on the hundred-bit is 0, there is no 1. If it is equal to 1, it depends on the low, such as 113, which can be broken down into 100-113, that is, 13 + 1 = low + 1. if it is greater than 1, for example, 813, it can be decomposed into 100-199, that is, 100

So what we need to solve now is to get the high (12 in 12013), the current bit cur (0 in 12013), and the low (13 in 12013)

Return to the important rule at the beginning. According to this rule, we need to use a variable to represent the current number of digits. The single digit is 1 and the ten digits are 10 .. And so on. It is represented by fac. fac starts from 1, and fac is multiplied by 10 after each processing.

 

Take 12013 as an example,

The calculation method for high positions is 12013/1000 = 12013/(BITS fac * 10) = 12

The current bit of cur is calculated as (12013/100) % 10 = (12013/120% bits fac) % 10 = 10 = 0

The low-level low calculation method is 12013-(12013/100) * 100 = 12013-(12013/12013 bits fac) * 12000 bits fac =-= 13.

 

There are three possibilities for the total number of hundred bits and one of the following:

1. When 100 bits are 0, total = high * fac = 12*1200 =

2. When 100 bits are set to 1, total = high * fac + low + 1 = 12 * + 13 + 1

3. When the number of BITs is other, total = high * fac + fac = (high + 1) * fac = (12 + 1) * 100

 

1 package com. rui. microsoft; 2 3 public class Test30_Count1 {4 5 public static void main (String [] args) {6 7 long current = System. currentTimeMillis (); 8 System. out. println ("TOTAL 1:" + countAll (120131111); 9 System. out. println ("time used:" + (System. currentTimeMillis ()-current); 10 11 current = System. currentTimeMillis (); 12 System. out. println ("TOTAL 1:" + cal (120131111); 13 System. out. prin Tln ("time used:" + (System. currentTimeMillis ()-current); 14} 15 16 public static int cal (int n) {17 int total = 0; 18 int fac = 1; 19 int high = 0, cur = 0, low = 0; 20 21 while (n/fac! = 0) {22 // 12013-> 12013-12000 = 1323 low = n-(n/fac) * fac; 24 // 12013-> (12013/100) % 10 = 025 cur = (n/fac) % 10; 26 // 12013-> 12013/(100*10) = 1227 high = n/(fac * 10 ); 28 29 switch (cur) {30 case 0: 31 // If the hundred bits are 032 // then the number of 1 bits on the hundred bits depends entirely on the high bits 33 total + = high * fac; 34 break; 35 case // 137 bits // The number of 1 bits on a hundred bits depends on the high bits and the low bits 38 total + = high * fac + low + 1; 39 break; 40 default: 41 // 142 bits greater than total + = (high + 1) * fac; 43 break; 44} 45 46 fac * = 10; 47} 48 49 50 return total; 51} 52 53 54 55 public static int countAll (int n) {56 if (n <= 0) return 0; 57 int total = 0; 58 for (int I = 1; I <= n; I ++) {59 total + = count (I); 60} 61 return total; 62} 63 64 private static int count (int num) {65 int total = 0; 66 67 while (num> 0) {68 if (num % 10 = 1) {69 total ++; 70} 71 num = num/10; 72} 73 74 return total; 75} 76 77}

 

Use the traditional method and the preceding method to calculate a long number, such as 120131111. The result is as follows:

TOTAL 1: 124234672
Time used: 1372
TOTAL 1: 124234672
Time used: 0

 

It can be seen that the performance gap is large.

Through this question, I thoroughly found the feeling of getting the remainder ....

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.