Number of 1 in algorithm 1~n

Enter an integer n and find the number of occurrences of 1 in the decimal representation of n integers from 1 to n.

For example, if you enter 12, the numbers containing 1 in the integers from 1 to 12 are 1, 10, 11, and 12, and 1 appears 5 times in total.

Example 1:

Input: n = 12

Output: 5

Example 2:

Input: n = 13

Output: 6

Source of the original question: https://leetcode-cn.com/problems/number-of-digit-one/
Algorithm 1: Calculate the number of 1s in each number, and then add all the numbers of 1s from 1 to n Up (low efficiency)

int NembOf1(int n)
{
    
    
	int ans = 0;
	while (n)
	{
    
    
		if (n % 10 == 1)
			ans++;
		n /= 10;
	}
	return ans;
}
int NembOf1BetweenN(int n)
{
    
    
	int ans = 0;
	for (int i = 1;i <= n;i++)
	{
    
    
		ans += NembOf1(i);
	}
	return ans;
}

Algorithm 2: See https://leetcode-cn.com/problems/number-of-digit-one/solution/c-kan-dao-bie-ren-fen-xiang-de-by-coke-10 shared by others / .
law given on beauty programming: 1. If the i-th bit (from right to left, beginning from the reference numeral 1) is the number 0, the number of possible bit i 1 bit is determined by a higher occurrence (if not high , Regard the high digit as 0), which is equal to the weight of the higher digit X current digit 10^(i-1). 2. If the number on the i-th digit is 1, the number of times a 1 may appear on the i-th digit is not only affected by the higher digits, but also by the lower digits (if there is no lower digit, the lower digit is regarded as 0), which is equal to the higher digit X current position The weight of the number is 10^(i-1)+ (lower number +1). 3. If the number on the i-th digit is greater than 1, the number of possible occurrences of 1 on the i-th digit is only determined by the higher digits (if there is no high digit, the higher digit is regarded as 0), which is equal to (higher digits + 1) X current digits The weight of 10^(i-1).

General conclusion:

From 1 to 10, in their single digits, any X appears once.
From 1 to 100, in their tens digits, any X appears 10 times.
From 1 to 1000, in their single digits. In thousands of digits, any X appears 100 times
and so on, from 1 to 10^i. In their second digit from the left (i-th digit from the right), any X appears 10^( i−1) times
The algorithm obtained is as follows:

When calculating the number of X contained in the i-th position from the right:

Take the number on the left (high) of the i-th place and multiply it by 10^(i−1) to get the basic value a

Take the i-th digit and calculate the correction value:

If it is greater than X, the result is a+10^(i−1).
If less than X, the result is a.
If equal to X, take the right (lower) digit of the i-th digit and set it as b, and the final result is a+b+ 1

class Solution {
    
    
public:
    int countDigitOne(int n) {
    
    
        if(n<1) return 0;
        int res=0,digit=1;
        int right=0,now,left;
        while(n){
    
    
            now=n%10;
            left=n/10;
            if(now<1){
    
    
                res+=left*pow(10,digit-1);
            }
            else if(now==1){
    
    
                res+=left*pow(10,digit-1)+right+1;
            }
            else{
    
    
                res+=(left+1)*pow(10,digit-1);
            }
            right+=pow(10,digit-1)*now;
            digit++;
            n/=10;
        }
        return res;
    }
};


Guess you like

Origin blog.csdn.net/Gunanhuai/article/details/109263360