A simple C solution[Accepted]


  • 0
    B
    int findNthDigit(int n) {
        if(n<10){return n;}
        long long temp1=0;
        long long temp2=0;
        int count=0;
        long long num=1;
        long long num1=0;
        long long num2=0;
        while(n>temp2){
            count++;
            num1=num2;
            num2=num2*10+9;
            temp1=temp2;
            temp2=(num2-num1)*count+temp1;  
        }
        for(int i=0;i<count-1;i++){num=num*10;}
        long long quotient =(n-temp1-1)/count;
        long long remainder=(n-temp1)%count;
        if(remainder==0){remainder=count;}
        long long num3=quotient+num;
        for(int i=0;i<remainder-1;i++){
            num3=num3%num;
            num=num/10;
        }
        return num3/num;
    }
    

Log in to reply
 

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