数字以0123456789101112131415…的格式序列化到一个字符序列中。在这个序列中,第5位(从下标0开始计数)是5,第13位是1,第19位是4,等等。
var findNthDigit = function (n) {
if (n < 10) return n;
var len = 1;
var weight = 1;
while (n > 9 * len * weight) {
n = n - 9 * len * weight;
len += 1;
weight *= 10;
}
var curNum = Math.floor(weight + (n - 1) / len);
var count = Math.floor((n - 1) % len);
return Math.floor((curNum / Math.pow(10, len - count - 1)) % 10);
};
console.log(findNthDigit(14));