我不行了,如果有更好的麻烦救救孩子。
不能导入 math 包?顺带复习简单快速幂
Go 的类型真的很严格。
func digitAtIndex(n int) int {
n -= 1
for digit := 1; digit <= 10; digit ++ {
first := pow(10, digit-1)
if n < 9 * first * digit {
conv := strings.Split(strconv.Itoa(first + n / digit), "")[n % digit]
res, _ := strconv.Atoi(conv)
return res
}
n -= 9 * first * digit
}
return 0
}
func pow(base, exp int) int {
r := 1
for exp != 0 {
if exp & 1 == 1 {
r = r * base
}
base = base * base
exp = exp >> 1
}
return r
}