题解 | #数字序列中某一位的数字#
数字序列中某一位的数字
http://www.nowcoder.com/practice/29311ff7404d44e0b07077f4201418f5
常规想法定义数字的相关的位数,进行对应的数位操作
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param n int整型
# @return int整型
#
class Solution:
def findNthDigit(self , n: int) -> int:
# write code here
# 找这种数字出现的规律
# 记录n 在第几个位数上面
# n是几位数
d = 1
# 区间的开始的数字
s = 1
# 记录当前区间有多少位数字
sum_m = 9
while n>sum_m:
n = n-sum_m
s = s*10
d = d+1
sum_m = 9*s*d
# 定位n在哪一个数字上,需要注意下标减去1
num = s + (n-1)//d
# 定义n在数字的哪一位上面
index = (n-1)%d # 多位数的第几位
return int(str(num)[index])
注意编程思路的实现