Skip to content

Latest commit

 

History

History
27 lines (22 loc) · 755 Bytes

File metadata and controls

27 lines (22 loc) · 755 Bytes

Screen Shot 2022-09-15 at 20 33 32

/**
 * @param {string} num1
 * @param {string} num2
 * @return {string}
 */
var multiply = function(num1, num2) {
    if(num1 === "0" || num2 === "0") return "0";
    let m = num1.length, n = num2.length, res = new Array(m + n).fill(0);
    
    for(let i = m - 1; i >= 0; i--) {
        for(let j = n - 1; j >= 0; j--) {
            let p1 = i + j, p2 = i + j + 1;
            let sum = res[p2] + +num1[i] * +num2[j];
            res[p2] = sum % 10;
            res[p1] += Math.floor(sum / 10);
        }
    }
    if(res[0] === 0) res.shift();
    return res.join("");
};