-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
29 lines (29 loc) · 1.16 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
/***
* 12345678910111213
* 规律个位数9个数一共有9*1,两位数90个数 一共有90*2个数字,三位数有900个数一共有900*3个数字,以此类推
* 举例15,15-9=6,6/2=3...0,余数是0,那么这个数值value=10*(2-1)+(3-1)=12,整除取最后一位 12%10=2
* 举例14,14-9=5,5/2=2...1,余数不为0,那么这个数值value=10*(2-1)+2=12,则为这个数的第余数个 12/(10*(2-1))%10=1
*/
public int findNthDigit(int n) {
long max = 9;
long num = n;
long digits = 1;
while (n > 0) {
if (num - max * digits > 0) {
num = num - max * digits;
digits++;
max = max * 10;
} else {
long count = num / digits;
long childDigits = num % digits;
if (childDigits == 0) {
return (int) (((long) Math.pow(10, digits - 1) + count - 1) % 10);
} else {
return (int) (((long) Math.pow(10, digits - 1) + count) / ((long) Math.pow(10, (digits - childDigits))) % 10);
}
}
}
return 0;
}
}