Skip to content

Commit 89b3eb3

Browse files
authored
Merge pull request halfrost#203 from gostool/leetcode0400
Leetcode0400
2 parents 6a96686 + 6b6942c commit 89b3eb3

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode
2+
3+
import "math"
4+
5+
func findNthDigit(n int) int {
6+
if n <= 9 {
7+
return n
8+
}
9+
bits := 1
10+
for n > 9*int(math.Pow10(bits-1))*bits {
11+
n -= 9 * int(math.Pow10(bits-1)) * bits
12+
bits++
13+
}
14+
idx := n - 1
15+
start := int(math.Pow10(bits - 1))
16+
num := start + idx/bits
17+
digitIdx := idx % bits
18+
return num / int(math.Pow10(bits-digitIdx-1)) % 10
19+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question400 struct {
9+
para400
10+
ans400
11+
}
12+
13+
// para 是参数
14+
type para400 struct {
15+
n int
16+
}
17+
18+
// ans 是答案
19+
type ans400 struct {
20+
ans int
21+
}
22+
23+
func Test_Problem400(t *testing.T) {
24+
25+
qs := []question400{
26+
27+
{
28+
para400{3},
29+
ans400{3},
30+
},
31+
32+
{
33+
para400{11},
34+
ans400{0},
35+
},
36+
}
37+
38+
fmt.Printf("------------------------Leetcode Problem 400------------------------\n")
39+
40+
for _, q := range qs {
41+
_, p := q.ans400, q.para400
42+
fmt.Printf("【input】:%v 【output】:%v\n", p.n, findNthDigit(p.n))
43+
}
44+
fmt.Printf("\n\n\n")
45+
}

leetcode/0400.Nth-Digit/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# [400. Nth Digit](https://leetcode-cn.com/problems/nth-digit/)
2+
3+
## 题目
4+
5+
Given an integer n, return the nth digit of the infinite integer sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...].
6+
7+
**Example 1**:
8+
9+
Input: n = 3
10+
Output: 3
11+
12+
**Example 2**:
13+
14+
Input: n = 11
15+
Output: 0
16+
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.
17+
18+
**Constraints:**
19+
20+
- 1 <= n <= int(math.Pow(2, 31)) - 1
21+
22+
## 题目大意
23+
24+
给你一个整数 n ,请你在无限的整数序列 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...] 中找出并返回第 n 位数字。
25+
26+
## 解题思路
27+
28+
- bits = 1的时候有1,2,3,4,5,6,7,8,9这9个数;9 = math.Pow10(bits - 1) * bits
29+
- bits = 2的时候有10-99这90个数;90 = math.Pow10(bits - 1) * bits
30+
- n不断减去bits从1开始的数字总数,求出n所在的数字是几位数即bits
31+
- 计算n所在的数字num,等于初始值加上(n - 1) / bits
32+
- 计算n所在这个数字的第几位digitIdx等于(n - 1) % bits
33+
- 计算出digitIdx位的数字
34+
35+
### 以11为例:
36+
11 - 9 = 2
37+
38+
(2 - 1) / 2 = 0
39+
40+
(2 - 1) % 2 = 1
41+
42+
也就是说第11位数字是位数是2的第一个数字的第二位,即是0
43+
44+
## 代码
45+
46+
```go
47+
package leetcode
48+
49+
import "math"
50+
51+
func findNthDigit(n int) int {
52+
if n <= 9 {
53+
return n
54+
}
55+
bits := 1
56+
for n > 9*int(math.Pow10(bits-1))*bits {
57+
n -= 9 * int(math.Pow10(bits-1)) * bits
58+
bits++
59+
}
60+
idx := n - 1
61+
start := int(math.Pow10(bits - 1))
62+
num := start + idx/bits
63+
digitIdx := idx % bits
64+
return num / int(math.Pow10(bits-digitIdx-1)) % 10
65+
}
66+
```

0 commit comments

Comments
 (0)