Share my javascript with dot handling O(n*m)


  • 1
    F

    The question only say that input is non-negative so I try to handle it :P

    /**
     * @param {string} num1
     * @param {string} num2
     * @return {string}
     */
    /**
     * Taste:
     * big number.
     * Try to handle dot.
     */
    'use strict';
    var ZERO_CHAR = '0'.charCodeAt(0);
    var multiply = function(num1, num2) {
      // Special cases.
      if ('0' === num1 || '0' === num2) {
        return '0';
      } else if ('1' === num1) {
        return num2;
      } else if ('1' === num2) {
        return num1;
      }
    
      var l1 = num1.length
          ,l2 = num2.length;
    
      // Store result as array first.
      var result = [];
    
      // Find if there is dot, if exist, remove it from string and save its digit.
      var p1 = num1.indexOf('.')
          ,p2 = num2.indexOf('.');
      if (p1 >= 0) {
        num1 = num1.slice(0, p1) + num1.slice(p1 + 1);
        p1 = l1 - 1 - p1;
        --l1;
      } else {
        p1 = 0;
      }
      if (p2 >= 0) {
        num2 = num2.slice(0, p2) + num2.slice(p2 + 1);
        p2 = l2 - 1 - p2;
        --l2;
      } else {
        p2 = 0;
      }
      var p = p1 + p2;
    
      // Big Number time starts!.
      var i, j;
      var d1, d2, digit;
      var v1, v2, multi;
      for (i = 0; i < l1; ++i) {
        v1 = num1.charCodeAt(i);
        if (ZERO_CHAR === v1) {
          continue;
        }
        v1 -= ZERO_CHAR;
        d1 = l1 - 1 - i;
        for (j = 0; j < l2; ++j) {
          v2 = num2.charCodeAt(j);
          if (ZERO_CHAR === v2) {
            continue;
          }
          v2 -= ZERO_CHAR;
          d2 = l2 - 1 - j;
          multi = v1 * v2;
          digit = d1 + d2;
          // Store the raw value in the target ditits, we'll handle it at once later
          if (typeof result[digit] === 'undefined') {
            result[digit] = multi;
          } else {
            result[digit] += multi;
          }
        }
      }
    
      // Fill undefined cell with 0, advance ditigs that larger than 10;
      var l = result.length;
      for (i = 0; i < l; ++i) {
        if (typeof result[i] === 'undefined') {
          result[i] = 0;
        } else if (result[i] >= 10) {
          if (typeof result[i+1] === 'undefined') {
            result[i + 1] = 0;
          }
          result[i + 1] += Math.floor(result[i] / 10);
          result[i] = result[i] % 10;
        }
      }
    
      // If p exist, put dot to target digit.
      if (p > 0) {
        result.splice(p1 + p2, 0, '.');
        ++l;
        // Start to trim useless zero value until dot is met, if no useful number,
        // remove dot as well.
        for (i = 0; i <= p; ++i) {
          if (result[i] === 0 || result[i] === '.') {
            continue;
          }
          break;
        }
        result = result.slice(i);
      }
      // Reverse array so we get correct number order.
      result.reverse();
    
      // Form the array to string.
      return result.join('');
    };

Log in to reply
 

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