-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfindNthDigit.go
More file actions
64 lines (52 loc) · 1.28 KB
/
Copy pathfindNthDigit.go
File metadata and controls
64 lines (52 loc) · 1.28 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* https://leetcode.com/problems/nth-digit/#/description
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
Note:
n is positive and will fit within the range of a 32-bit signed integer (n < 2^31).
Example 1:
Input:
3
Output:
3
Example 2:
Input:
11
Output:
0
Explanation:
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.
1-9 9
10-99 90 * 2
100-999 900 * 3
1000-9999 9000 * 4
*/
package lmath
func findNthDigit(n int) int {
// 找出第n个digit所在的数
temp, digit := 0, 1
for ; temp < n; digit++ {
temp += 9 * powForfindNthDigit(10, digit-1) * digit
}
digit -= 1
temp -= 9 * powForfindNthDigit(10, digit-1) * digit
num := powForfindNthDigit(10, digit-1) - 1 + (n-temp)/digit // 待修正
// 找到第n个digit在num中的index
// 如果index=0,则表示目标digit在num的最后位置,否则在对应的index位置,所以实际index =index -1
index := (n - temp) % digit
if index != 0 {
num += 1
} else {
index = digit
}
// 找到目标digit,按下标取之
for i := index; i < digit; i++ {
num /= 10
}
return num % 10
}
func powForfindNthDigit(x, y int) int {
r := 1
for i := 0; i < y; i++ {
r *= x
}
return r
}