[Careercup] 18.4 count number of 2

Source: Internet
Author: User

18.4 Write a method to count the number of 2s between 0 and N.

This problem gives us an integer n, let us find all 2 occurrences of the [0,n] interval, such as if n=20, then meet test instructions is 2, 12, 20, then return 3. Leetcode on a very similar topic factorial Trailing zeroes, but the number of the question to 5 also includes the factor of 5, such as 10 inside there are 5, which is the difference between the two questions. So first this problem can be solved with brute force, we call a function for each number within the interval, to count the number of 2 occurrences in that figure. and statistics a number of people on the number is very simple, is the flat shift, to 10, if 2, then the counter increases by 1, and then this number is divided by 10, until 0 stop, see the code as follows:

Solution One:

intCount_number_2 (intnum) {    intres =0;  while(Num >0) {        if(num%Ten==2) ++Res; Num/=Ten; }    returnRes;}intCount_in_range (intnum) {    intres =0;  for(inti =2; I <= num; ++i) {res+=count_number_2 (i); }    returnRes;}

In fact, there is a better way to do this problem, we are not in the interval of a number of 2, but the bitwise to find, for example, we first to list some of the sequence:

0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
...
110 111 112 113 114 115 116 117 118 119

We found that the minimum number of occurrences of the 2 frequency is 1 per 10 digits, and we can probably be divided into three scenarios to discuss, digit < 2, Digti = 2, and digit > 2.

Solution Two:

intCount_in_range_as_digit (intNumintd) {intPower_of_10 = POW (Ten, D); intNEXT_POWER_OF_10 = power_of_10 *Ten; intright = num%power_of_10; intRound_down = num-num%next_power_of_10; intROUND_UP = Round_down +next_power_of_10; intdigit = (num/power_of_10)%Ten; if(Digit <2)returnRound_down/Ten; Else if(Digit = =2)returnRound_down/Ten+ Right +1; Else returnROUND_UP/Ten;}intCount_in_range (intnum) {    intres =0; intLen =to_string (num). Size ();  for(inti =0; i < Len; ++i) {res+=count_in_range_as_digit (num, i); }    returnRes;}

Careercup all in one topic summary

[Careercup] 18.4 count number of 2

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.