9 line C++ simple solution


  • 0
    E
    int findNthDigit(int n) {
    	long long int scale = 1, len = 1;
    	while (n > 9 * scale * len)
    	{
    		n -= 9 * scale * len;
    		scale *= 10;
    		len++;
    	}
    	//"scale" will be the next starting number
    	string str_num = to_string(scale + (n - 1) / len); //calculate the number which the digit is in
    	return str_num[(n - 1) % len] - '0'; //get the Nth digit of the number
    }
    

Log in to reply
 

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