Skip to content

Commit 5ad9c50

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 17.8 MB (39.35%)
1 parent 10b74d5 commit 5ad9c50

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Solutions/0400-nth-digit/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<p>Given an integer <code>n</code>, return the <code>n<sup>th</sup></code> digit of the infinite integer sequence <code>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]</code>.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
6+
<pre>
7+
<strong>Input:</strong> n = 3
8+
<strong>Output:</strong> 3
9+
</pre>
10+
11+
<p><strong class="example">Example 2:</strong></p>
12+
13+
<pre>
14+
<strong>Input:</strong> n = 11
15+
<strong>Output:</strong> 0
16+
<strong>Explanation:</strong> The 11<sup>th</sup> 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.
17+
</pre>
18+
19+
<p>&nbsp;</p>
20+
<p><strong>Constraints:</strong></p>
21+
22+
<ul>
23+
<li><code>1 &lt;= n &lt;= 2<sup>31</sup> - 1</code></li>
24+
</ul>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def findNthDigit(self, n: int) -> int:
3+
val, count = 1, 9
4+
while val * count < n:
5+
n -= val * count
6+
val += 1
7+
count *= 10
8+
9+
num = 10 ** (val - 1) + ((n - 1) // val)
10+
index = (n - 1) % val
11+
return int(str(num)[index])

0 commit comments

Comments
 (0)