<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: sugar free</title>
    <description>The latest articles on DEV Community by sugar free (@sugar_free_2622cec27ea539).</description>
    <link>https://dev.to/sugar_free_2622cec27ea539</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3561458%2F5df1b246-ae4a-483a-a534-e0b40561f33f.png</url>
      <title>DEV Community: sugar free</title>
      <link>https://dev.to/sugar_free_2622cec27ea539</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sugar_free_2622cec27ea539"/>
    <language>en</language>
    <item>
      <title>LeetCode 400: Nth Digit — Python Solution &amp; Explanation</title>
      <dc:creator>sugar free</dc:creator>
      <pubDate>Wed, 15 Oct 2025 13:22:29 +0000</pubDate>
      <link>https://dev.to/sugar_free_2622cec27ea539/leetcode-400-nth-digit-python-solution-explanation-50b1</link>
      <guid>https://dev.to/sugar_free_2622cec27ea539/leetcode-400-nth-digit-python-solution-explanation-50b1</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/nth-digit/" rel="noopener noreferrer"&gt;Leetcode 400 -- Nth Digit&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Analysis
&lt;/h2&gt;

&lt;p&gt;Firstly, it's easy to observe that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Numbers 0-9 have only 1 digit,&lt;/li&gt;
&lt;li&gt;Numbers 10-99 have 2 digits,&lt;/li&gt;
&lt;li&gt;Numbers 100-999 have 3 digits, and so on.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In other words:&lt;br&gt;
There are 9*1(9e0*1) digits in 1-digit numbers, 90*2(9e1*2) digits in 2-digit numbers, 900*3(9e2*3) digits in 3-digit numbers...&lt;/p&gt;

&lt;p&gt;Then we can easily get an array that contains all digits for each group of i-digit numbers&lt;br&gt;
&lt;code&gt;bounds = [9e0, 9e1*2, 9e2*3, 9e3*4, 9e4*5, 9e5*6, 9e6*7, 9e7*8, 9e8*9, 9e9*10, 9e10*11]&lt;/code&gt;&lt;br&gt;
This allows us to quickly determine how many digits we need to reach the group that contains the target digit.&lt;/p&gt;
&lt;h3&gt;
  
  
  Example 1:
&lt;/h3&gt;

&lt;p&gt;n = 10 so it's obvious that 10 could cover 9*1 with 1 remaining, which is not enough to cover 90*2. So when n = 10, the result &lt;strong&gt;must be in a 2-digit number and it's the 1st digit of all 2-digit numbers. which is 1 in number 10.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Example 2:
&lt;/h3&gt;

&lt;p&gt;n = 1000 it's also obvious that 1000 could cover 9*1 and 90*2 , with remaining 1000 - 9 - 180 = 811 digits, which is not enough to cover 900*3, so obviously the &lt;strong&gt;result mush be in a 3-digit number and it's the 811th digit of all 3-digit numbers.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;Based on the analysis above ,the next steps are clear.&lt;br&gt;
Once we know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the digits of the number(&lt;strong&gt;digit_range&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;the remaining digits count(&lt;strong&gt;remaining_digits&lt;/strong&gt;)
We can find the number that contains the result:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;digit_range&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;remaining_digits&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;digit_range&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;remaining_digits&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;digit_range&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  note:
&lt;/h3&gt;

&lt;p&gt;if the remaining_digits is exactly divisible by digit_range, number should &lt;strong&gt;decrease by one&lt;/strong&gt;&lt;br&gt;
 e.g. digit_range = 2, remaining_digits = 2, the result should be the 2nd digit of the number 10 which is 0&lt;br&gt;
 but if we don't minus it would be 10*1 + 2//2 = 11, which is wrong.&lt;/p&gt;

&lt;p&gt;Now we get the number, the final step is to get the digit_index of the result within the number, which is quite simple:&lt;br&gt;
&lt;code&gt;digit_index = remaining_digits % digit_range&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For easily get the digit of each index, we convert the number into str.&lt;br&gt;
Since Python (and most programming languages) use &lt;strong&gt;0-based indexing&lt;/strong&gt;,&lt;br&gt;
we can safely convert the number to a string and extract the digit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;numberStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numberStr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;digit_index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And because in python, index -1 goes to the end of the iterable, so for those remainings that are fully divided by digit_range, the result automatically goes to the last digit, which is compeletely the correct answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Full Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def findNthDigit(self, n: int) -&amp;gt; int:
        bounds = [9e0, 9e1*2, 9e2*3, 9e3*4, 9e4*5, 9e5*6, 9e6*7, 9e7*8, 9e8*9, 9e9*10, 9e10*11]
        digits = [1,2,3,4,5,6,7,8,9,10]
        digit_range = 0 # how many digits in the number
        for i in range(0,11,1):
            if n &amp;gt; bounds[i]:
                n -= bounds[i]
            else:
                digit_range = digits[i]
                break
        remaining_digits = n
        number = 10 ** (digit_range-1) + remaining_digits // digit_range
        if remaining_digits % digit_range == 0:
            number = number - 1
        digit_index = remaining_digits % digit_range # the index of the digit in the number
        numberStr = str(int(number))
        return int(numberStr[int(digit_index)-1])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>interview</category>
      <category>algorithms</category>
      <category>python</category>
      <category>leetcode</category>
    </item>
  </channel>
</rss>
