mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 17:44:10 +08:00
20 lines
351 B
Go
20 lines
351 B
Go
package leetcode
|
|
|
|
import "math"
|
|
|
|
func findNthDigit(n int) int {
|
|
if n <= 9 {
|
|
return n
|
|
}
|
|
bits := 1
|
|
for n > 9*int(math.Pow10(bits-1))*bits {
|
|
n -= 9 * int(math.Pow10(bits-1)) * bits
|
|
bits++
|
|
}
|
|
idx := n - 1
|
|
start := int(math.Pow10(bits - 1))
|
|
num := start + idx/bits
|
|
digitIdx := idx % bits
|
|
return num / int(math.Pow10(bits-digitIdx-1)) % 10
|
|
}
|