Posted on

Description

Submission

class Solution {
public:
    int findNthDigit(int n) {
        int k = 1;
        for(; true; ++k) {
            long long t = (pow(10, k) - pow(10, k - 1)) * k;
            if(n >= t) {
                n -= t;
            } else {
                break;
            }
        }
        if(n == 0) return 9;

        int base = pow(10, k - 1);

        int nthNumber = n / k;
        int nthDigit = n % k;

        if(nthDigit == 0) {
            nthNumber -= 1;
            nthDigit = k;
        }

        return to_string(base + nthNumber)[nthDigit-1] - '0';
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *