题目链接
思路
- 确定数位的量级

比如1525,
1∗0.9∗101+2∗0.9∗102+3∗0.9∗103+4∗0.9∗104>1525
1∗0.9∗101+2∗0.9∗102+3∗0.9∗103<1525
即
9+90+900+9000>1525>9+90+900
可见对应的digit = 3
- 找到所对应的num
n=1525−9−90−900
num=103+(n−1)/3,n-1为了从1001计数
- num转为string,找到对应的下标
index=(n−1)%3
代码实现
class Solution:
def findNthDigit(self, n: int) -> int:
digit = 1
count = 1
while digit * 0.9 * pow(10, digit) < n:
n -= digit * 0.9 * pow(10, digit)
digit += 1
s = str(pow(10, digit-1) + (n-1)//digit)
return int(s[int((n-1)%digit)])