每日一题力扣400努力看懂了

 

在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 位数字。

class Solution:
    def findNthDigit(self, n: int) -> int:
        if n<10:
            return n
        
        num=9
        cnt=1
        while n>num*cnt:#看落在哪个区间,1位数有9个数字,2位数有90*2个数字(包括上一区间就是9+90*2个),3位数有900*3个数字(包括前二区间就是9+90*2+900*3个数),
            n-=num*cnt#用n减去最小区间的数,看看剩下的数是否大于下一区间的数,大于的话就属于下下区间
            cnt+=1#下一区间的位数
            num*=10
        target=num//9+(n-1)//cnt
        #计算目标数
        target=num//9 + (n-1)//cnt
        index=(n-1)%cnt
        return str(target)[index]

 

posted @ 2021-03-12 16:17  小千北同学超爱写代码  阅读(67)  评论(0编辑  收藏  举报