/**
* @author xnl
* @Description:
* @date: 2022/7/6 22:43
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.findNthDigit(3));
}
public int findNthDigit(int n) {
int digit = 1; // 位数
long start = 1; // 起点大小
long count = 9; // 当前位的最大值、
// 找到所属的位置
while (n > count){
n -= count;
start *= 10;
digit += 1;
count = start * digit * 9;
}
// 定位到对应的数字
long num = start + (n - 1) / digit;
// 定位到对数字对应的下标
return Long.toString(num).charAt((n - 1) % digit) -'0';
}
}
力扣:剑指 Offer 44. 数字序列中某一位的数字
最新推荐文章于 2024-08-10 22:52:22 发布