class Solution {
public:
int findNthDigit(int n) {
int d = 1, count = 9;
while (n > (long) d * count) {
n -= d * count;
d++;
count *= 10;
}
int index = n - 1;
int start = (int) pow(10, d - 1);
int num = start + index / d;
int digitIndex = index % d;
int digit = (num / (int) (pow(10, d - digitIndex - 1))) % 10;
return digit;
}
};
class Solution {
public int findNthDigit(int n) {
int d = 1, count = 9;
while (n > (long) d * count) {
n -= d * count;
d++;
count *= 10;
}
int index = n - 1;
int start = (int) Math.pow(10, d - 1);
int num = start + index / d;
int digitIndex = index % d;
int digit = (num / (int)(Math.pow(10, d - digitIndex - 1))) % 10;
return digit;
}
}
class Solution {
public int findNthDigit(int n) {
//特殊情况
if (n == 0) {
return 0;
}
long bit = 1;
int i = 1;
long count = 9;
//确定是哪个区间
while(count < n) {
n = (int)(n - count);
bit = bit * 10;
i++;
count = bit * i * 9;
}
//确定是区间上的哪个数
long num = bit + (n - 1) / i;
//确定是数上的第几位
int index = (n - 1) % i + 1;
int res = (int)(num / Math.pow(10, i - index)) % 10;
return res;
}
}
//不是很懂
class Solution {
public:
int findNthDigit(int n) {
if(n==0) return 0;
long bit = 1;
long count = 9;
int i=1;
while(count<n)
{
n-=count;
bit*=10;
i+=1;
count = bit*9*i;
}
long num = bit+(n-1)/i;
int index = (n-1)%i+1;
int res = (int)(num/pow(10,i-index))%10;
return res;
}
};
class Solution {
public:
int findNthDigit(int n) {
if(n < 10){
return n;
}
long bit = 1;
int i = 1;
long count = 9;
while(count < n){
n -= count;
bit *= 10;
i++;
count = bit * i * 9;
}
// 确定是在这个区间的哪个数
long num = bit + (n - 1) / i;
// 确定在 Num 的第几个字符
int index = (n - 1) % i + 1;
int res = (int)(num / pow(10, i - index)) % 10;
return res;
}
};
共以下 9 个回答
不是很懂
题解学习
看了题解
1、添零补位法,没有全部AC,69/70
2、