Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... 

Note:
n is positive and will fit within the range of a 32-bit signed integer (n < 231).


Example 1:

Input:
3

Output:
3



Example 2:

Input:
11

Output:
0

Explanation:
The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10.

Source: leetcode | Difficulty: EASY
Tags: Sorting

Mathematics

Starter code: class Solution:
    def findNthDigit(self, n: int) -> int:
        

Solutions:
class Solution:

	def findNthDigit(self, n):
		i = count = 9
		while count < n:
			i *= 10
			count += i * len(str(i))
		(div, mod) = divmod(n - (count - i * len(str(i))), len(str(i)))
		print(i, count, div, mod)
		target = i // 9 - 1 + div
		if mod == 0:
			print(target, int(str(target)[-1]))
			return int(str(target)[-1])
		else:
			return int(str(target + 1)[mod - 1])


class Solution:

	def findNthDigit(self, n):
		digs = 1
		while n > 9 * 10 ** (digs - 1) * digs:
			n -= 9 * 10 ** (digs - 1) * digs
			digs += 1
		return int(str(int(10 ** (digs - 1) + (n - 1) / digs))[n % digs - 1])


class Solution:

	def findNthDigit(self, n):
		digitCount = 1
		totalNumbersWithDigitCount = 9 * 10 ** (digitCount - 1) * digitCount
		while n > totalNumbersWithDigitCount:
			n -= totalNumbersWithDigitCount
			digitCount += 1
			totalNumbersWithDigitCount = 9 * 10 ** (digitCount - 1) * digitCount
		baseNumber = 10 ** (digitCount - 1) - 1
		targetNumber = baseNumber + math.ceil(n / digitCount)
		targetDigitIndex = n % digitCount - 1
		return int(str(targetNumber)[targetDigitIndex])


class Solution:

	def findNthDigit(self, n):
		start = 0
		digit = 0
		while True:
			temp = 9 * 10 ** digit
			if temp * (digit + 1) < n:
				n -= temp * (digit + 1)
				start += 10 ** digit * 9
				digit += 1
			else:
				break
		step = int(n / (digit + 1))
		start += step
		n -= (digit + 1) * step
		if n == 0:
			return start % 10
		start += 1
		return int(start / 10 ** (digit + 1 - n)) % 10


class Solution:

	def findNthDigit(self, n):
		n -= 1
		for digits in range(1, 11):
			first = 10 ** (digits - 1)
			if n < 9 * first * digits:
				return int(str(first + n / digits)[n % digits])
			n -= 9 * first * digits


class Solution:

	def findNthDigit(self, n):
		(i, count) = (1, 9)
		total = 9
		while total < n:
			i += 1
			count *= 10
			total += i * count
		n -= total - i * count + 1
		return int(str(n // i + 10 ** (i - 1))[n % i])


class Solution:

	def findNthDigit(self, n):
		if n <= 0:
			return
		i = 0
		nbase = 0
		thres = 1 * 9
		if n <= thres:
			return n
		while 1:
			i += 1
			nbase += 9 * 10 ** (i - 1)
			thres += (i + 1) * 9 * 10 ** i
			if n <= thres:
				return int(str((n + i - (thres - (i + 1) * 9 * 10 ** i)) // (i + 1) + nbase)[(n + i - (thres - (i + 1) * 9 * 10 ** i)) % (i + 1)])


class Solution:

	def findNthDigit(self, n):
		multiple = math.floor(math.log10(n))
		subtract = sum([math.pow(10, i) for i in range(0, multiple)])
		boundary = multiple * math.pow(10, multiple) - subtract
		diff = n - boundary
		if diff > 0:
			x = math.pow(10, multiple) - 1 + math.ceil(diff / (multiple + 1))
			p = (diff - 1) % (multiple + 1)
		elif diff <= 0:
			x = math.pow(10, multiple) + math.ceil(diff / multiple) - 1
			p = (diff - 1) % multiple
		r = [ch for ch in str(x)][int(p)]
		return int(r)


class Solution:

	def findNthDigit(self, n):
		g = 1
		while True:
			g_len = (10 ** g - 10 ** (g - 1)) * g
			if n <= g_len:
				break
			n -= g_len
			g += 1
		idx = (n - 1) // g
		pos = (n - 1) % g
		num = 10 ** (g - 1) + idx
		return int(str(num)[pos])


class Solution:

	def findNthDigit(self, n):
		n_digit = 1
		while n > n_digit * 9 * 10 ** (n_digit - 1):
			n -= n_digit * 9 * 10 ** (n_digit - 1)
			n_digit += 1
		n_pre = (n - 1) // n_digit
		digit_position = (n - 1) % n_digit + 1
		num = 10 ** (n_digit - 1) + n_pre
		tmp = 0
		num = num % 10 ** (n_digit - digit_position + 1) // 10 ** (n_digit - digit_position)
		return num