python solution


  • 0
    W

    Basic idea

    • find how many digits of number needed, say m
    • subtract the largest value of m-1 digits from n to obtain the number of digits between
    • divide by m to obtain the number of integers in the distance
    • find the desired number and digits
    import math
    class Solution(object):
        def findNthDigit(self, n):
            """
            :type n: int
            :rtype: int
            """
            if n < 10:
              return n
            else:
              digits = []
              i = 0
              s = 0
              while s < n:
                s += (i + 1) * 9 * 10 ** i
                i += 1
                digits.append(s)
              n -= digits[i-2]
              d = int(math.ceil(1.0 * n / i))
              p = n % i
              return int(str(10**(i - 1) - 1 + d)[p-1])
    

Log in to reply
 

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