Java solution beats 89.1%


  • 1
    Z
    public int findNthDigit(int n) {
        int m = 0, i = 1;
        while (n > 0 && i < 10) {
            m = n;
            n -= i * 9 * Math.pow(10, i - 1);
            i++;
        }
        i--;
        int divide = m / i;
        if (divide == 0) {
            divide = 1;
        } else if (divide * i < m) {
            divide++;
        }
        int index = m % i;
        if (index == 0) {
            index = i;
        }
        int target = (int) Math.pow(10, i - 1) + divide - 1;
        return target % (int) Math.pow(10, i - index + 1) / (int) Math.pow(10, i - index);
    }

Log in to reply
 

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