class Solution:
def findNthDigit(self, n: int) -> int:
digit_length = 1
count = 9
start = 1
# Find the range in which the nth digit falls
while n > digit_length * count:
n -= digit_length * count
digit_length += 1
count *= 10
start *= 10
# Find the actual number where the nth digit is located
num = start + (n - 1) // digit_length
# Find the digit in the number
digit_index = (n - 1) % digit_length
return int(str(num)[digit_index])
class Solution {
public:
int findNthDigit(int n) {
long long digitLength = 1, count = 9, start = 1;
while (n > digitLength * count) {
n -= digitLength * count;
digitLength++;
count *= 10;
start *= 10;
}
int num = start + (n - 1) / digitLength;
int digitIndex = (n - 1) % digitLength;
string numStr = to_string(num);
return numStr[digitIndex] - '0';
}
};
class Solution {
public int findNthDigit(int n) {
int digitLength = 1;
long count = 9;
int start = 1;
while (n > digitLength * count) {
n -= digitLength * count;
digitLength++;
count *= 10;
start *= 10;
}
int num = start + (n - 1) / digitLength;
int digitIndex = (n - 1) % digitLength;
return Integer.toString(num).charAt(digitIndex) - '0';
}
}
var findNthDigit = function(n) {
let digitLength = 1, count = 9, start = 1;
while (n > digitLength * count) {
n -= digitLength * count;
digitLength++;
count *= 10;
start *= 10;
}
let num = start + Math.floor((n - 1) / digitLength);
let digitIndex = (n - 1) % digitLength;
return Number(num.toString()[digitIndex]);
};
Given an integer n
, you are to find the n
th digit in the infinite sequence of all positive integers concatenated together: 123456789101112...
and so on.
For example, the 3rd digit is 3
, the 11th digit is 0
(from 10).
Constraints:
n
≤ 231 - 1
At first glance, it might seem like you should build the sequence by concatenating numbers until you reach the n
th digit. However, this is very inefficient, especially for large n
(up to 2 billion).
Instead, we need to find a way to "jump" directly to the region of the sequence that contains the n
th digit.
The key insight is that numbers with the same digit length contribute a predictable amount of digits. For example:
The optimized solution follows these steps:
count * digit_length
). Subtract this from n
until the group containing the n
th digit is found.
start
) for that group, and the offset, we can compute which number contains the n
th digit.
start + (n - 1) // digit_length
(n - 1) % digit_length
.
Example: n = 15
n = 15
is greater than 9, so subtract 9: n = 6
n = 6
is less than 180, so the digit is in this group.(n-1) // 2 = (6-1)//2 = 2
. So, 2 numbers after 10: 10, 11, 12(n-1) % 2 = (6-1)%2 = 1
Brute-force: O(n) time and space, since you would need to build the sequence up to the n
th digit.
Optimized Approach:
n
(which is at most 10 for 32-bit integers).
The problem asks for the n
th digit in an infinite concatenation of positive integers. The key insight is to avoid brute-force concatenation and instead use arithmetic to "jump" to the correct digit's group, then compute the exact number and digit. This results in a fast, elegant solution with logarithmic time complexity and constant space usage.