mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
add: leetcode 0400 solution
This commit is contained in:
19
leetcode/0400.Nth-Digit/400.Nth Digit.go
Normal file
19
leetcode/0400.Nth-Digit/400.Nth Digit.go
Normal file
@ -0,0 +1,19 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user