코테준비 - Number of Digit One

정상화·2023년 2월 26일

LeetCode

목록 보기
203/222

Number of Digit One


class Solution {
public:
    int countDigitOne(int n) {
        if(n == 1'000'000'000){
            return 900000001;
        }
        int p, d, r;
        int cnt = 0;
        for (int i = 0; i < 9; i++) {
            d = pow(10, i);
            if(n < d){
                break;
            }
            p = (n + 1 - d) / (10 * d);
            r = (n + 1 - d) % (10 * d);
            cnt += p * d + R(r,d);
        }

        return cnt;
    }

    int R(int r, int d){
        if(r <= d) {
            return r;
        }
        return d;
    }
};
profile
백엔드 희망

0개의 댓글