In the infinite sequence of integers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... found in the n-th digit.
Note:
n-and is a positive number in the range of 32 to the shaping (n <231).
Example 1:
Input:
3
Output:
3
Example 2:
Input:
11
Output:
0
Description:
The first 11 digits is 0 in the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ..., it is the part 10.
solution:
func findNthDigit(_ n: Int) -> Int {
var n = n
var carry = 1
var bits = 1
while n > carry * bits * 9 {
n -= carry * 9 * bits
carry += 1
bits *= 10
}
let num = (n - 1)/carry + bits
let mol = ((carry - n % carry) % carry) + 1
var dev = 1
for _ in 0..<mol-1 {
dev *= 10
}
return ((num % (dev * 10)) / dev)
}