【剑指Offer】JZ44 数字序列中某一位的数字

JZ44 数字序列中某一位的数字

描述

 数字以 0123456789101112131415... 的格式作为一个字符序列,在这个序列中第 2 位(从下标 0 开始计算)是 2 ,第 10 位是 1 ,第 13 位是 1 ,以此类题,请你输出第 n 位对应的数字。

# @param n int整型 
# @return int整型
#
class Solution:
    def findNthDigit(self , n: int) -> int:
    	#先确认是几位
    	#[0] 1
    	#[1-9] 1*9
    	#[10-99] 2*90
    	#[100-999] 3*900
    	#[1000-9999] 4*9000
    	if n == 0:
    		return 0
    	count = 9
    	digit = 1
    	start = 1
    	while n > count:
    		n = n - count
    		start = start * 10
    		digit = digit + 1
    		count = start * 9 * digit
    	#以start开始的  n/digit 可以知道第几个数,
    	num = start + (n-1)/digit
    	a = (n-1)%digit
    	s = str(num)
    	return int(s[a])

    	#找到哪个数
    	#这个数的第几位
a = Solution()
print(a.findNthDigit(100000000000))
posted @ 2022-08-01 15:37  一半丶  阅读(38)  评论(0编辑  收藏  举报