C++ Simple Solution 10 lines


  • 0

    Inspired by @Mad_air 's code

    class Solution {
    public:
        int findNthDigit(int n) {
            long cnt = 9, digit = 1;
            while (n - cnt * digit > 0) {
                n -= cnt * digit;
                cnt *= 10;
                digit++;
            }
            int index = (n - 1) % digit, val = (n - 1) / digit;
            long long target = val + pow(10, digit - 1);
            return to_string(target)[index] - '0';
        }
    };
    

Log in to reply
 

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