面试题43
示例 1:
输入:n = 12
输出:5
示例 2:
输入:n = 13
输出:6限制:
注意:本题与主站 233 题相同:https://leetcode-cn.com/problems/number-of-digit-one/
Solutions
Last updated
示例 1:
输入:n = 12
输出:5
示例 2:
输入:n = 13
输出:6Last updated
class Solution {
public:
int countDigitOne(int n) {
long res = 0;
for (long num = 1; num <= n; num *= 10) {
long base = num * 10;
res += (n / base) * num + min(max((n % base) + 1 - num, 0l), num);
}
return res;
}
};