Description
Given an integer n
, return the nth
digit of the infinite integer sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]
.
Examples:
Example 1:
Input: n = 3
Output: 3
Example 2:
Input: n = 11
Output: 0
Explanation: The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10.
Solution in Python
To solve the problem of finding the nth digit in the infinite sequence of integers, we can break it into several steps:
- Identify the Range of the nth Digit:
- Digits in the sequence are grouped by the number of digits in the numbers:
- Single-digit numbers (1–9): 9 numbers, contributing 9×1=9 digits.
- Two-digit numbers (10–99): 90 numbers, contributing 90×2=180 digits.
- Three-digit numbers (100–999): 900 numbers, contributing 900×3=2700 digits.
- Continue this pattern until the range containing nn is identified.
- Digits in the sequence are grouped by the number of digits in the numbers:
- Find the Exact Number:
- Once the range is identified, determine the exact number containing the nth digit by calculating the offset of nn within the range.
- Extract the Digit:
- Use modular arithmetic or string slicing to extract the desired digit from the identified number.
Python
class Solution:
def findNthDigit(self, n: int) -> int:
# Step 1: Determine the range where the nth digit lies
digit_length = 1 # Current digit length (1 for 1-9, 2 for 10-99, etc.)
count = 9 # Number of integers with the current digit length
start = 1 # Starting number of the current digit length
# Reduce n until it fits within a digit range
while n > digit_length * count:
n -= digit_length * count
digit_length += 1
count *= 10
start *= 10
# Step 2: Identify the exact number containing the nth digit
number_index = (n - 1) // digit_length # Zero-based index of the number
digit_index = (n - 1) % digit_length # Zero-based index of the digit within the number
target_number = start + number_index # The actual number containing the digit
# Step 3: Extract the desired digit
return int(str(target_number)[digit_index])
Explanation of the Code
- Step 1: Locate the Range:
- Start with single-digit numbers (11-digit length, 99 numbers).
- Progressively move to higher digit ranges, subtracting the total digit count for each range until n fits in a range.
- Step 2: Identify the Number:
- Calculate the index of the number within the identified range.
- Use start+number_index to compute the exact number containing the nth digit.
- Step 3: Extract the Digit:
- Convert the number to a string to access its digits.
- Use the digit_index to find the specific digit in the string representation.
Solution in C++
C++
class Solution {
public:
int findNthDigit(int n) {
// Step 1: Determine the range of numbers where the nth digit is located
long long digitLength = 1; // Start with single-digit numbers
long long count = 9; // Total digits in this range (9 for single digits)
long long start = 1; // Starting number in this range
// Keep incrementing the range until the nth digit is found
while (n > digitLength * count) {
n -= digitLength * count; // Subtract the total digits in the current range
digitLength++; // Move to the next range (e.g., 2-digit numbers)
count *= 10; // Update the count (e.g., 90 for 2-digit numbers)
start *= 10; // Update the starting number (e.g., 10 for 2-digit numbers)
}
// Step 2: Locate the exact number and the digit within that number
long long number = start + (n - 1) / digitLength; // Find the number
int digitIndex = (n - 1) % digitLength; // Find the digit's index in the number
// Step 3: Extract the digit from the number
std::string numberStr = std::to_string(number); // Convert the number to a string
return numberStr[digitIndex] - '0'; // Convert the character to an integer
}
};
Solution in Javascript
JavaScript
var findNthDigit = function(n) {
// Step 1: Define variables to track the digit range, count, and starting number for each digit length
let digitLength = 1; // Start with single-digit numbers
let count = 9; // There are 9 single-digit numbers (1 to 9)
let start = 1; // Single-digit numbers start at 1
// Step 2: Narrow down the range where the nth digit falls
while (n > digitLength * count) {
n -= digitLength * count; // Subtract the digits covered by the current range
digitLength += 1; // Move to the next digit length (e.g., 2-digit, 3-digit, etc.)
count *= 10; // Update count (e.g., 90 for 2-digit numbers, 900 for 3-digit numbers)
start *= 10; // Update start (e.g., 10 for 2-digit numbers, 100 for 3-digit numbers)
}
// Step 3: Determine the exact number and digit within the identified range
const targetNumber = start + Math.floor((n - 1) / digitLength); // Calculate the target number
const digitIndex = (n - 1) % digitLength; // Determine the position of the digit within the number
// Step 4: Return the nth digit
return Number(String(targetNumber)[digitIndex]); // Convert the number to a string, extract the digit, and return it
};
Solution in Java
Java
class Solution {
public int findNthDigit(int n) {
// Step 1: Determine the range of digits (1-digit, 2-digit, etc.)
// Initialize variables
int digitLength = 1; // The length of numbers in this range
long count = 9; // The count of numbers in this range
long start = 1; // The starting number of this range
// While `n` is greater than the total digits in the current range
while (n > digitLength * count) {
n -= digitLength * count; // Move past this range
digitLength++; // Move to the next range (e.g., 1-digit -> 2-digit)
count *= 10; // Update the count of numbers in this range
start *= 10; // Update the starting number of this range
}
// Step 2: Identify the exact number that contains the nth digit
long number = start + (n - 1) / digitLength; // Calculate the actual number
// Step 3: Find the specific digit within the number
String numberString = Long.toString(number); // Convert the number to a string
int digitIndex = (n - 1) % digitLength; // Determine the index of the digit in the string
// Return the digit as an integer
return Character.getNumericValue(numberString.charAt(digitIndex));
}
}