Kind of intuitive Python solution


  • 0
    N
    class Solution(object):
        def findNthDigit(self, n):
            """
            :type n: int
            :rtype: int
            """
            if n < 10:
                return n
        
            k = len(str(n))
            ranges = [1]  # number of digits in numbers in [10^i-1, 10^i)
            n -= sum(ranges)
            for i in xrange(1, k + 1):
                ranges.append(i * 9 * 10 ** (i - 1))
        
                if n < ranges[i]:
                    number, digit = divmod(n, i)
                    order = 10 ** (i - 1)
                    return int(str(order + number)[digit])
                n -= ranges[i]
    

Log in to reply
 

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