思路:先找到n对应的区间在哪里,定位start
从start的0位开始数,第n个数组。
remain=start+(n-1)/digit;
index=(n-1)%digit;
return to_string(remain)[index];
class Solution {
public:
int findNthDigit(int n) {
if(n<=9) return n;
n-=9;
int start=10;
int end=start*10-1;
int digit=2;
int len=(end-start+1)*digit;
while(n-len>0) {
n-=len;
start*=10;
end=start*10-1;
digit++;
len=(end-start+1)*digit;
}
int remain=start+(n-1)/digit;
int index=(n-1)%digit;
return to_string(remain)[index]-'0';
}
};