题目链接:https://leetcode-cn.com/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/
难度:中等

描述:
数字以0123456789101112131415…的格式序列化到一个字符序列中。在这个序列中,第5位(从下标0开始计数)是5,第13位是1,第19位是4,等等。

请写一个函数,求任意第n位对应的数字。

题解

  1. class Solution:
  2. def findNthDigit(self, n: int) -> int:
  3. digit, start, count = 1, 1, 9
  4. while n > count:
  5. n -= count
  6. start *= 10
  7. digit += 1
  8. count = 9 * start * digit
  9. num = start + (n - 1) // digit
  10. return int(str(num)[(n - 1) % digit])