publicclassSolution { publicintFindNthDigit(int n) { int low = 1, high = 9; while (low < high) { int mid = (high - low) / 2 + low; if (TotalDigits(mid) < n) { low = mid + 1; } else { high = mid; } } int d = low; int prevDigits = TotalDigits(d - 1); int index = n - prevDigits - 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; }
classSolution { public: intfindNthDigit(int n){ int low = 1, high = 9; while (low < high) { int mid = (high - low) / 2 + low; if (totalDigits(mid) < n) { low = mid + 1; } else { high = mid; } } int d = low; int prevDigits = totalDigits(d - 1); int index = n - prevDigits - 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; }
为了方便计算目标数字,使用目标数字在所有 $d$ 位数中的下标进行计算,下标从 $0$ 开始计数。令 index} = n - 1$,则 index 即为目标数字在所有 $d$ 位数中的下标,index 的最小可能取值是 $0$。
得到下标 index 之后,即可使用方法一的做法得到无限整数序列中的第 $n$ 位数字。
[sol2-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { publicintfindNthDigit(int n) { intd=1, count = 9; while (n > (long) d * count) { n -= d * count; d++; count *= 10; } intindex= n - 1; intstart= (int) Math.pow(10, d - 1); intnum= start + index / d; intdigitIndex= index % d; intdigit= (num / (int)(Math.pow(10, d - digitIndex - 1))) % 10; return digit; } }
[sol2-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicclassSolution { publicintFindNthDigit(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; } }
[sol2-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: intfindNthDigit(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; } };
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var findNthDigit = function(n) { let d = 1, count = 9; while (n > d * count) { n -= d * count; d++; count *= 10; } const index = n - 1; const start = Math.floor(Math.pow(10, d - 1)); const num = start + Math.floor(index / d); const digitIndex = index % d; const digit = Math.floor(num / Math.floor(Math.pow(10, d - digitIndex - 1))) % 10; return digit; };
[sol2-Golang]
1 2 3 4 5 6 7 8 9 10 11 12
funcfindNthDigit(n int)int { d := 1 for count := 9; n > d*count; count *= 10 { n -= d * count d++ } index := n - 1 start := int(math.Pow10(d - 1)) num := start + index/d digitIndex := index % d return num / int(math.Pow10(d-digitIndex-1)) % 10 }
[sol2-Python3]
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: deffindNthDigit(self, n: int) -> int: d, count = 1, 9 while n > d * count: n -= d * count d += 1 count *= 10 index = n - 1 start = 10 ** (d - 1) num = start + index // d digitIndex = index % d return num // 10 ** (d - digitIndex - 1) % 10