Nth Digit in Whole Numbers

Title: Nth DigitSource: leetcode.com

Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …

n is positive and will fit within the range of a 32-bit signed integer (n < 231).

Python solution

The intuition for the solution is thinking about what’s the count of numbers having d digits.
For example, there are 9 numbers (1-9) having 1 digit, 90 numbers (10-99) having 2 digits and so on.
Once we have this, we know how many digits are there in each range of numbers. Based on that we find the numbers which would have the required digit.

Rate this post

Leave a Reply