400 - Nth Digit
Written on November 12, 2017
Tweet
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …
class Solution(object):
def findNthDigit(self, n):
"""
:type n: int
:rtype: int
"""
start, size, step = 1, 1, 9
#find the size of the number where the nth digit is from
while n > size * step:
n -= size * step
size += 1
step *= 10
start *= 10
#find the actual number where the nth digit is from
start += (n - 1) / size
#find the nth digit and return
return int(str(start)[(n - 1) % size])