Sword refers to Offer 43. The number of occurrences of 1 in 1~n integer (C++) High and low ideas

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:

输入:n = 12
输出:5

Example 2:

输入:n = 13
输出:6

Restrictions:
1 <= n <2^31
Note: This question is the same as the 233 question on the main website: https://leetcode-cn.com/problems/number-of-digit-one/

Problem-solving ideas:

Insert picture description here
How to calculate the number of occurrences of 1 in a certain position:

According to the different cur value of the current bit, it can be divided into the following three situations:

1. When cur = 0 : The number of occurrences of this bit 1 is only determined by the high bit high. The calculation formula is:
high×digit
as shown in the figure below. Taking n = 2304 as an example, find the 1 of digit = 10 (that is, the tens digit) The number of occurrences.
Insert picture description here
2. When cur = 1 : The number of occurrences of this bit 1 is determined by the high bit and the low bit low. The calculation formula is:
high×digit+low+1
as shown in the figure below. Taking n = 2314 as an example, find digit = 10 ( That is, the number of occurrences of 1 in the tens place.
Insert picture description here
3. When cur = 2, 3,...,9 : The number of occurrences of this bit 1 is only determined by the high bit high. The calculation formula is:

					(high+1)×digit

As shown in the figure below, taking n = 2324 as an example, find the number of 1 occurrences of digit = 10 (that is, the tens place).
Insert picture description here
Insert picture description here

class Solution {
    
    
public:
    int countDigitOne(int n) {
    
    
        long digit = 1;
        int res = 0;
        int high = n / 10, cur = n % 10, low = 0;//cur是n除以10的余数,后面的cur是high的余数
        while(high != 0 || cur != 0) {
    
    // 当 high 和 cur 同时为 0 时,说明已经越过最高位,因此跳出
            if(cur == 0) res += high * digit;
            else if(cur == 1) res += high * digit + low + 1;
            else res += (high + 1) * digit;//
            low += cur * digit;//low就是cur一轮一轮叠加得到;此处参数是用作下一轮
            cur = high % 10;//cur是high的余数;;下一轮用
            high /= 10;//high从右边减少一位;下一轮用
            digit *= 10;//digit乘以10;下一轮用
        }
        return res;

    }
};

Complexity analysis:

Time complexity O(log n): The calculation operation in the loop uses O(1) time; the number of loops is the number of digits of the number n, that is, log 10 n, so the loop uses O(log n) time.
Space complexity O(1): Several variables use extra space of constant size.

Author: jyd
link: https: //leetcode-cn.com/problems/1nzheng-shu-zhong-1chu-xian-de-ci-shu-lcof/solution/mian-shi-ti-43-1n-zheng-shu -zhong-1-chu-xian-de-2/
Source: LeetCode (LeetCode)
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/qq_30457077/article/details/114759466