1. class Solution {
    2. public:
    3. int countDigitOne(int n) {
    4. int count = 0;
    5. int high, low;
    6. long long digit;
    7. high = n;
    8. low = 0;
    9. digit = 1;
    10. while (high > 0) {
    11. int i = high % 10;
    12. high /= 10;
    13. count += (high * digit);
    14. if (i > 1)
    15. count += digit;
    16. else if (i == 1)
    17. count += (low + 1);
    18. digit *= 10;
    19. low = n % digit;
    20. }
    21. return count;
    22. }
    23. };