this is an interesting problem, here is my solution in Java


  • 0
    G
    public class Solution {
        public int findNthDigit(int n) {
            int inputN=n;
            int bitW=1;    
            int bitWNums=9;
            while((long)n > (long) bitWNums*bitW){   // without long will be overflow
                n -= (bitWNums * bitW);
                bitW++;
                bitWNums *= 10;
            }
            long base=(long)(Math.pow(10,bitW-1));
            /*
            while(n>bitW){
                base ++;
                n-=bitW;
            }*/
            base += n/bitW;
            if(n%bitW == 0 && n!= 0){
                base -=1;
                n=bitW;
            }else{
                n=n%bitW;
            }
            return (int)(getBitOfNum(base,n,bitW));
        }
        private long getBitOfNum(long num,int index,int numW){
            index = numW-index;
            return ((long)(num/Math.pow(10,index)))%10;
        }
    }
    

    i thing this problem is interesting and a little complex.


Log in to reply
 

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