share my cpp solution


  • -1
    C

    class Solution {
    public:
    int findNthDigit(int n) {
    long long lln = (long long)n;
    long long ret = 0,gap = 9,cnt = 0,wei = 1;
    while(cnt+gapwei<lln){
    cnt = cnt+gap
    wei;
    wei++;
    gap = gap10;
    }
    if(cnt+gap
    wei==n) return 9;
    long long num = (n-cnt-1)/wei+pow(10,wei-1);
    long long one = (n-cnt-1)%wei;
    stringstream ss;
    ss<<num;
    string str = ss.str();
    return str[one]-'0';
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.