dirty javascript solution


  • 0
    S
    /**
     * @param {number} n
     * @return {number}
     * ---------------------------------------
     * nums of                        total
     * the block                      digits
     * ---------------------------------------
     * 9           1     ~  9             9
     * 90          10    ~  99          189
     * 900         100   ~  999        2889
     * 9000        1000  ~  9999      38889
     * ---------------------------------------
     */
    var findNthDigit = function(n) {
        
        var digits_of_a_num = 0;
        var total_digits = 0;
    
        do{
            digits_of_a_num++;
            
            if (digits_of_a_num === 1) {
                total_digits = 9;
            }
            else{
                total_digits = Number( (digits_of_a_num -1).toString() + '8'.repeat(digits_of_a_num -1) + '9');
            }
            
        }while(n > total_digits);
    
    
        if (digits_of_a_num === 1) {
            return n;
        }
        else {
    
    
    
    
            var one_before_block_total_digits = 0;
            
            if (digits_of_a_num === 2) {
                one_before_block_total_digits = 9;
            }
            else{
                one_before_block_total_digits = Number( (digits_of_a_num -2).toString() + '8'.repeat(digits_of_a_num -2) + '9');
            }
            
            
            var one_before_block_num = 0;
            
            one_before_block_num = Number( '9'.repeat(digits_of_a_num - 1) );
        
            
            var additional_num = Math.floor(   (n - one_before_block_total_digits) / digits_of_a_num  );
            
            var nth_digit_of_the_num = (n - one_before_block_total_digits) % digits_of_a_num
            
            if (nth_digit_of_the_num === 0){
                return Number(   (one_before_block_num + additional_num).toString().slice(-1)   );
            }
            else {
                return Number(   (one_before_block_num + additional_num + 1).toString().charAt(nth_digit_of_the_num -1 )   );
            }
    
    
    
        }
    
    };
    

Log in to reply
 

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