LeetCode—剑指Offer:数字序列中某一位的数字(规律)

数字序列中某一位的数字(中等)

2020年9月5日

题目来源:力扣
在这里插入图片描述
解题
规律题真没思路,看大佬题解

class Solution {
    
    
    public int findNthDigit(int n) {
    
    
        //参数初始化
        //数位
        int digit = 1;
        //开始位
        long start = 1;
        //数位数量
        long count = 9;
        //参数重置
        while (n > count) {
    
    
        	//使n变成digit数位中从1开始的数值
            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'; 
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41541562/article/details/108414748