Skip to content

Commit 7b274c7

Browse files
committed
add leetcode: offer-44
1 parent 99357ba commit 7b274c7

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package offer;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-26-23:51
6+
* @Function Description :44.数字序列中某一位的数字
7+
*/
8+
public class _44ANumberInADigitalSequence {
9+
class Solution {
10+
public int findNthDigit(int n) {
11+
// n所在的位数
12+
int digit = 1;
13+
// start当前遍历的起始位置 count当前遍历相同位数的数字共有多少个
14+
long start = 1 , count = 9 ;
15+
while( n > count ){
16+
n -= count;
17+
digit += 1;
18+
start *= 10;
19+
count = digit * start * 9;
20+
}
21+
// 第n为数字出现在num中某一位上
22+
long num = start + (n - 1) / digit;
23+
return Long.toString(num).charAt( (n - 1) % digit ) - '0';
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)