0%

剑指 Offer 44. 数字序列中某一位的数字

剑指 Offer 44. 数字序列中某一位的数字

又是一道数学的找规律类型的题,好烦
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int findNthDigit(int n) {
long digit = 1, count = 9, start = 1;
while(count < n)
{
n -= count;
start *= 10;
++digit;
count = 9 * digit * start;
}
// 0的时候直接计算出来是0,不会影响结果
int num = (n - 1) / digit + start;
return to_string(num)[(n - 1) % digit] - '0';
}
};

1625218647824.png 1625218657922.png 1625218664563.png