Javascript solution, if anyone is interested


  • 4
    P
    var productExceptSelf = function(nums) {
        var output = [];
        var leftMult = 1;
        var rightMult = 1;
        for (var i=nums.length - 1; i >= 0; i--) {
            output[i] = rightMult;
            rightMult *= nums[i];
        }
        for (var j=0; j < nums.length; j++) {
            output[j] *= leftMult;
            leftMult *= nums[j];
        }
        return output;
    };

  • 0
    M
    This post is deleted!

  • 0
    E

    My inefficient solution with map() and reduce();

    const productExceptSelf = function(nums) {
         return nums.map(function (curr, i, arr) {
             let temp = arr.slice()
             temp.splice(i, 1)
             return temp.reduce((product = 1, num) => product * num)
            })
    };
    

Log in to reply
 

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