劍指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])

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章