数字序列中某一位的数字(中等)
2020年9月5日
class Solution {
public int findNthDigit(int n) {
//参数初始化
//数位
int digit = 1;
//开始位
long start = 1;
//数位数量
long count = 9;
//参数重置
while (n > count) {
//使n变成digit数位中从1开始的数值
n -= count;
digit += 1;
start *= 10;
//数位数量的规律
count = digit * start * 9;
}
//确定数字
long num = start + (n - 1) / digit;
//确定数位
return Long.toString(num).charAt((n - 1) % digit) - '0';
}
}