400. Nth Digit
Find the n th digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
Note: n is positive and will fit within the range of a 32-bit signed integer ( n < 231).
Example 1:
Input:
3
Output:
3
Example 2:
Input:
11
Output:
0
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.
# @lc code=start
using LeetCode
function find_nth_digit(n::Int)
ant = [0, 10, 190, 2890, 38890, 488890, 5888890, 68888890, 788888890]
num_begin = [0, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000]
idx = searchsortedlast(ant, n)
num = (n - ant[idx]) ÷ idx + num_begin[idx]
posi = (n - ant[idx]) % idx
return num % (10^(idx - posi)) ÷ 10^(idx - posi - 1)
end
# @lc code=end
find_nth_digit (generic function with 1 method)
This page was generated using DemoCards.jl and Literate.jl.