- Medium
- Given an integer
n
, return thenth
digit of the infinite integer sequence[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]
.
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])