-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy path44.java
More file actions
26 lines (26 loc) · 955 Bytes
/
44.java
File metadata and controls
26 lines (26 loc) · 955 Bytes
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
class Solution {
public int findNthDigit(int n) {
if(n==0)
return 0;
//数字的长度为len; 从长度为1的数字开始, 也就是从个位数开始
int len = 1;
//长度为len的数字有count个
long count = 9;
//长度为len的第一个数字
int start = 1;
//确定第n位对应的数字的长度
while(n > len*count){
// n = n - len*count; //这么写会报错, 需要把count转成int
n -= len*count; //这么写就不报错了
//update
len++;
start = start*10;
count = count*10;
}
//确定第n位对应的数字是哪个长度为len的数字
start = start + (n%len==0? n/len-1 : n/len);
//取出该数字的第(n-1)%len位
String s = Integer.toString(start);
return Character.getNumericValue(s.charAt(n%len==0? len-1 : n%len-1));
}
}