一、题目地址
https://leetcode-cn.com/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/
二、具体代码
/**
* @param {number} n
* @return {number}
*/
// 迭代 + 求整
// 时间复杂度:O(logn)
// 空间复杂度:O(logn)
var findNthDigit = function(n) {
let start = 1, digit = 1, count = 9;
// 1、计算n所在的数字是几位数,即计算digit
while(n > count) {
n -= count;
digit += 1;
start *= 10;
count = start * 9 * digit;
}
// 2、计算n所在的数字是哪一个,即计算num
let num = start + Math.trunc((n - 1) / digit);
// 3、返回n在num中指向的数字是哪一个
return String(num).charAt((n - 1) % digit) - '0';
};