题解 | #数字序列中某一位的数字#
数字序列中某一位的数字
http://www.nowcoder.com/practice/29311ff7404d44e0b07077f4201418f5
//吃数学的亏
int findNthDigit(int n) {
// write code here
int dig =1;
//start 用long long 否则越界结果不对。
long long start = 1,count = 9;
while(n>count)
//原本考虑top-bottom,但是0-9和后面的10-99、100-999有点出入
{
n-=count;
start*=10;
dig++;
count = start*dig*9;
}
int num = start + (n-1)/dig;//n-1让下标从0开始,方便求商
return to_string(num)[(n-1)%dig]-'0';//可以不用细想,直接认为是求当前元素中的下标
}