Problem
Given an integern, return the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ....
Examples
Constraints
- 1 <= n <= 2^31 - 1
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Tested Python solution for LeetCode 400 with 29 pytest cases. Generate a practice environment with lcpy.
lcpy gen -n 400 # by problem number
lcpy gen -s nth_digit # by problem name
n, return the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ....
Input: n = 3
Output: 3
Input: n = 11
Output: 0
class Solution:
# Time: O(log n)
# Space: O(1)
def find_nth_digit(self, n: int) -> int:
digits = 1
count = 9
start = 1
while n > digits * count:
n -= digits * count
digits += 1
count *= 10
start *= 10
num = start + (n - 1) // digits
return int(str(num)[(n - 1) % digits])
| Time | Space |
|---|---|
| O(log n) | O(1) |