题解 | #数字序列中某一位的数字#(C++ 版本)

数字序列中某一位的数字

http://www.nowcoder.com/practice/29311ff7404d44e0b07077f4201418f5

class Solution { public:

int findNthDigit(int n) {
    int digCont = 1; //位数
    long long start = 1, count = 9;  // 0~9  10~99 100~999
    while(n > count) {
        n -= count;
        digCont += 1;
        start *= 10;
        count = start * digCont * 9;
    }
    int num = start + (n - 1) / digCont;
    string s = to_string(num);
    int result = s[(n - 1) % digCont] - '0';
    return result;
    
}

};

全部评论

相关推荐

2 收藏 评论
分享
牛客网
牛客企业服务