Simple C++ Solution, O(log(n)), 3 ms


  • 0
    M
    class Solution {
    public:
        int findNthDigit(int n) {
            --n;
            int64_t base = 1, cnt = 9, len = 1;
            while (true) {
                if (n < cnt * len) break;
                n -= cnt * len;
                base *= 10;
                cnt *= 10;
                ++len;
            }
            return to_string(base + n / len)[n % len] - '0';
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.