Update 400 solution

This commit is contained in:
halfrost
2021-12-09 21:21:21 +08:00
parent 89b3eb3b53
commit 5c81c0982f
10 changed files with 92 additions and 15 deletions

View File

@ -1,4 +1,4 @@
# [400. Nth Digit](https://leetcode-cn.com/problems/nth-digit/)
# [400. Nth Digit](https://leetcode.com/problems/nth-digit/)
## 题目
@ -25,25 +25,26 @@ Given an integer n, return the nth digit of the infinite integer sequence [1, 2,
## 解题思路
- bits = 1的时候有1,2,3,4,5,6,7,8,9这9个数;9 = math.Pow10(bits - 1) * bits
- bits = 2的时候有10-9990个数;90 = math.Pow10(bits - 1) * bits
- n不断减去bits从1开始的数字总数,求出n所在的数字是几位数即bits
- 计算n所在的数字num等于初始值加上(n - 1) / bits
- 计算n所在这个数字的第几位digitIdx等于(n - 1) % bits
- 计算出digitIdx位的数字
- bits = 1 的时候有 1,2,3,4,5,6,7,8,9 这 9 个数; 9 = math.Pow10(bits - 1) * bits
- bits = 2 的时候有 10-9990 个数; 90 = math.Pow10(bits - 1) * bits
- n 不断减去 bits 从 1 开始的数字总数,求出 n 所在的数字是几位数即 bits
- 计算 n 所在的数字 num等于初始值加上 (n - 1) / bits
- 计算 n 所在这个数字的第几位 digitIdx 等于 (n - 1) % bits
- 计算出 digitIdx 位的数字
### 以11为例:
### 以11 为例:
11 - 9 = 2
(2 - 1) / 2 = 0
(2 - 1) % 2 = 1
也就是说第11位数字是位数是2的第一个数字的第二位即是0
也就是说第 11 位数字是位数是 2 的第一个数字的第二位,即是 0
## 代码
```go
package leetcode
import "math"