Clean and neat java solution


  • 1
    F
        public int findNthDigit(int n) {
            int k = 1;
            long num = 9;
            int start = 1;
            while (n > k * num) {
                n -= k++ * num;
                num *= 10;
                start *= 10;
            }
            start += --n / k;
            return Integer.toString(start).charAt(n % k) - '0';
        }
    

    Because k*num may cause overflow, so use long for num.


  • 1

    Can you please explain the approach..
    specifically the start variable.


  • 0
    W

    said in Clean and neat java solution:

    public int findNthDigit(int n) {
    int k = 1;
    long num = 9;
    int start = 1;
    while (n > k * num) {
    n -= k++ * num;
    num *= 10;
    start *= 10;
    }
    start += --n / k;
    return Integer.toString(start).charAt(n % k) - '0';
    }

    Eligent


Log in to reply
 

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