Java solution


  • 0
    D
    public class Solution {
        public int findNthDigit(int n) {
            assert n > 0;
            int digitsSoFar = 0;
            int numDigits = 1;
            int beginNum = 1;
            for (long iLenNum = 9; numDigits <= 10; iLenNum *= 10, numDigits++, beginNum *= 10) {
                if (n <= digitsSoFar + iLenNum * numDigits) {
                    break;
                }
                digitsSoFar += iLenNum * numDigits;
            }
            n -= digitsSoFar;
            int num = beginNum + (n - 1) / numDigits;
            int nth = ((n - 1) % numDigits) + 1;
            int ret = 0;
            while (--nth >= 0) {
                ret = num / beginNum;
                num -= ret * beginNum;
                beginNum /= 10;
            }
            return ret;
        }
    }
    

Log in to reply
 

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