Skip to content

Commit 302931e

Browse files
committed
math
1 parent ec5f822 commit 302931e

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

400 Nth Digit.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
3+
4+
Note:
5+
n is positive and will fit within the range of a 32-bit signed integer (n < 231).
6+
7+
Example 1:
8+
9+
Input:
10+
3
11+
12+
Output:
13+
3
14+
Example 2:
15+
16+
Input:
17+
11
18+
19+
Output:
20+
0
21+
22+
Explanation:
23+
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.
24+
"""
25+
__author__ = 'Daniel'
26+
27+
28+
class Solution(object):
29+
def findNthDigit(self, n):
30+
"""
31+
Math, quotient and remainder
32+
:type n: int
33+
:rtype: int
34+
"""
35+
digit_cnt = 1
36+
num_cnt = 9
37+
while n > digit_cnt * num_cnt:
38+
n -= digit_cnt * num_cnt
39+
digit_cnt += 1
40+
num_cnt *= 10
41+
42+
n -= 1 # debugging: without -1, it just pass over the target digit
43+
q, r = n / digit_cnt, n % digit_cnt
44+
target = num_cnt / 9 + q
45+
return int(str(target)[r])

0 commit comments

Comments
 (0)