-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path400.py
41 lines (35 loc) · 1.11 KB
/
400.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
__________________________________________________________________________________________________
sample 20 ms submission
class Solution:
def findNthDigit(self, n: int) -> int:
length = 1
cnt = 9
start = 1
while n > cnt*length:
n -= cnt*length
length += 1
cnt *= 10
start *= 10
start += (n-1)//length
s = str(start)
return int(s[(n-1) % length])
__________________________________________________________________________________________________
sample 13064 kb submission
#
# @lc app=leetcode id=400 lang=python3
#
# [400] Nth Digit
#
class Solution:
def findNthDigit(self, n: int) -> int:
length = 1
times = 1
while n > length * 9 * times:
n -= length * 9 * times
length += 1
times *= 10
offset_num = (n-1) // length
offset_digit = (n-1) % length
digit = str(times + offset_num)[offset_digit]
return int(digit)
__________________________________________________________________________________________________