题解 | #数字序列中某一位的数字#
数字序列中某一位的数字
https://www.nowcoder.com/practice/29311ff7404d44e0b07077f4201418f5
// 居然做了一天 各种小错误 最后静下来慢慢调试过的
class Solution{
public:
int findNthDigit(int n) {
if(n==0) return 0;
int res = n;
int digit = 1;
long bound = 0;
while(1){
bound = 9*pow(10, digit-1)*(digit);
if(res > bound){
res = res - bound; // res是去掉前面固定的以后的剩下的数字量
digit++;
}
else break;
}
int nums = res/(digit) ; // 除以当前的位数 得到数字的个数
int prev = (digit>1) ? pow(10, digit-1) + nums : nums; // bound+数字个数
res = res % digit;
int cur = (res>0) ? prev + 1 : prev; // 得到当前的数字
string result = to_string(cur);
if(res>0) return result[res-1]-'0';
else return result.back()-'0';
}
};

