【leetcode 数学问题 C++】【剑指 Offer】 43. 1~n 整数中 1 出现的次数 233. Number of Digit One

剑指 Offer 43. 1~n 整数中 1 出现的次数

233. Number of Digit One

【leetcode 数学问题 C++】【剑指 Offer】 43. 1~n 整数中 1 出现的次数 233. Number of Digit One

class Solution {
public:
    int countDigitOne(int n) {
        uint32_t base = 1;
        uint32_t ans = 0;
        int N = n;
        while(n) {
            ans += n / 10 * base;
            if(n % 10) ans += base;
            if(n % 10 == 1) ans -= base - N % base - 1;
            base *= 10;
            n /= 10;
        }
        return ans;
    }
};
上一篇:LeetCode-233 Number of Digit One


下一篇:模拟python底层数据类型,大整数的实现!