0233.cpp (282B)
1 class Solution { 2 public: 3 int countDigitOne(int n) const { 4 int res = 0; 5 6 for (long long m = 1; m <= n; m *= 10) { 7 const int a = n / m, b = n % m; 8 res += (a + 8) / 10 * m + (a % 10 == 1) * (b + 1); 9 } 10 11 return res; 12 } 13 };