0
点赞
收藏
分享

微信扫一扫

C++ | Leetcode C++题解之第400题第N位数字

ivy吖 2024-09-13 阅读 17

题目:

题解:

class Solution {
public:
    int findNthDigit(int n) {
        int d = 1, count = 9;
        while (n > (long) d * count) {
            n -= d * count;
            d++;
            count *= 10;
        }
        int index = n - 1;
        int start = (int) pow(10, d - 1);
        int num = start + index / d;
        int digitIndex = index % d;
        int digit = (num / (int) (pow(10, d - digitIndex - 1))) % 10;
        return digit;
    }
};
举报

相关推荐

0 条评论