s4553711
2/20/2018 - 2:50 PM

400.cpp

class Solution {
public:
    int findNthDigit(int n) {
        long digit = 1, sum = 9;
        while(n > digit * sum) {
            n = n - digit * sum;
            sum *= 10;
            digit++;
        }
        int index = n % digit;
        if (index == 0) {
            index = digit;
        }
        long num = pow(10, digit - 1);
        num += (index == digit) ? (n/digit - 1) : (n/digit);
        for(int i = index; i < digit; i++) {
            num /= 10;
        }
        return num % 10;
    }
};