思路
本题解来自:面试题44. 数字序列中某一位的数字(迭代 + 求整 / 求余,清晰图解)
1 class Solution { 2 public: 3 int findNthDigit(int n) { 4 int digit = 1; 5 long long start = 1; 6 long long count = 9; 7 while (n > count) { // 1. 8 n -= count; 9 digit += 1; 10 start *= 10; 11 count = digit * start * 9; 12 } 13 long long num = start + (n - 1) / digit; // 2. 14 return to_string(num)[(n - 1) % digit] - '0'; // 3. 15 16 } 17 };