Skip to content

Latest commit

 

History

History
30 lines (24 loc) · 668 Bytes

400.-nth-digit.md

File metadata and controls

30 lines (24 loc) · 668 Bytes

400. Nth Digit

  • Medium
  • Given an integer n, return the nth digit of the infinite integer sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...].

Analysis

We can observe that

    1-9     | 9
   10-99    | 90
  100-999   | 900 
 1000-9999  | 9000 
10000-99999 | 90000

class Solution:
    def findNthDigit(self, n: int) -> int:
        digit = base = 1 # starting from 1 digit
        while n > 9*base*digit: # upper limit of d digits 
            n -= 9*base*digit
            digit += 1
            base *= 10 
        q, r = divmod(n-1, digit)
        return int(str(base + q)[r])