C++ solution fro Nth Digit


  • 0
    G
    class Solution{
    public:
        int findNthDigit(int n){
            long count=0,base=9;
            int level=0,pre;
            while(count<n&&count>=0){
                level++;
                pre=count;
                count+=level*base;
                base*=10;
            }
            int currDigit=n-pre-1;
            int currNum=(currDigit)/level+pow(10,level-1);
            int indexOfNum=level-1-currDigit%level;
            return (currNum/(int)pow(10,indexOfNum))%10;
        }
    };

Log in to reply
 

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