class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型
* @return int整型
*/
int findNthDigit(int n) {
// write code here
/*string res;
for(int i=0;res.length()<=n;i++) res += to_string(i); //运行超时
return res[n]-'0';*/
//0-9 不算0算9个 1位数
//10-99 90个2位数
//100-999 900个3位数 9000个4位数 900000个5位数
long long dig=1,start=1,count=9;
while(n>count){
n -= count;
start *= 10; //当前n位数的开头
dig += 1; //当前一个数字占的位数
count = start * dig * 9;
}
int num = start + (n - 1) / dig;
string s = to_string(num);
int result = s[(n - 1) % dig] - '0';
return result;
}
};