Simple Python Solution


  • 1
    T

    What if all numbers in the sequence have leading zeroes which makes they have same length? Yes, this will make the solution much easier. So...

    class Solution(object):
        def findNthDigit(self, n):
            """
            :type n: int
            :rtype: int
            """
            if n <= 9: return n
            i, p = 1, 9
            while True:
                n += p
                p = p * 10 + 9
                i += 1
                if n < p * i:
                    return int(str(int((n + i - 1) // i))[(n + i - 1) % i])
    

    Idea:

    • If n < 9 then ... the answer is trivial
    • if n > 9, pad first 9 number with an leading zero, so every number is 2 digits now ( n += p )
    • Then, if n < 99 * 2 the answer is trivial (note, here, n is modified)
    • if n > 99 * 2, pad first 99 number with an loading zero, ... (just repeat...)

  • 0
    J

    Great solution! Instead of subtracting from n, you are adding.


Log in to reply
 

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