剑指Offer(Python多种思路实现):数字序列中某一位的数字

剑指Offer(Python多种思路实现):数字序列中某一位的数字

面试44题:

题目描述:数字序列中某一位的数字
数字以0123456789101112131415…的格式序列化到一个字符序列中。在这个序列中,第5位(从0开始计数)是5,第13位是1,第19位是4,等等。请写一个函数求任意位对应的数字。

解题思路:

def findNthDigit(self, n):
    start, step, size = 1, 9, 1
    while n > size * step:
        n, start, step, size = n-size*step, start*10, step*10, size+1
    return int(str(start + (n-1)//size)[(n-1) % size])
发布了62 篇原创文章 · 获赞 6 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_44151089/article/details/104524695