当前位置 博文首页 > 来老铁干了这碗代码的博客:剑指 Offer 44. 数字序列中某一位的
class Solution {
public int findNthDigit(int n) {
int digit = 1; // 该数的位数
long start = 1; // 该位数的起始值,1位数的起始值为1, 2位数的起始值为10
long count = 9; // 某位数所有数字连在一起的长度。
while(n > count) {
n -= count;
digit += 1;
start *= 10;
count = digit * start * 9;
}
long num = start + (n-1) / digit;
return String.valueOf(num).charAt((n-1)%digit) - '0';
}
}
cs