序列中某一位的数字

题目

https://leetcode-cn.com/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof

解法

1
2
3
4
5
6
7
8
9
10
11
12
13
public int findNth(int n) {
int digit = 1;
long start = 1;
long count = 9; // 注意越界问题
while (n>count) {
n -= count;
digit += 1;
start *= 10;
count = digit * start * 9;
}
long num = start + (n-1)/digit;
return Long.toString(num).charAt((n-1)%digit) - '0';
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×