leetcode 400

来源:互联网 发布:单片机排针怎么用 编辑:程序博客网 时间:2024/05/02 15:04

水题,照着模拟即可

但容易写错,在 pow(10,cnt) 上忘记向上取整wa了几发

class Solution {public:    int findNthDigit(int n) {        int cnt = 1;        long long int nub = 9;        while( n > nub*cnt ){            n -= cnt*nub;            cnt++;            nub *= 10;        }        int pos = ceil(pow(10,cnt-1));        pos += n/cnt ;        n = n%cnt;        if( n == 0 )pos--,n = cnt;        int t = cnt - n;        while( pos ){            int x = pos%10;            if( t == 0 )return x;            t--;            pos/=10;        }        return pos;    }};


原创粉丝点击