题解 | #数字序列中某一位的数字#
数字序列中某一位的数字
https://www.nowcoder.com/practice/29311ff7404d44e0b07077f4201418f5
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param n int整型
# @return int整型
# pdd考过一个类似的
class Solution:
def findNthDigit(self , n: int) -> int:
# write code here
i,j = 1,0
while n - 9*i*(10**j) > 0:
n -= 9*i*(10**j)
i += 1 # 跑到几位数了
j += 1 # 1开头多少0
cur_num = 10**j + (n-1) // i # 开头的1000...加上 剩余的n//i,就是这个数的数值
cur_pos = (n-1) % i # n的余数就是n的第几位
return int(str(cur_num)[cur_pos]) # 取出来转数字
查看30道真题和解析