My solution with javascript


  • 2
    M
        var productExceptSelf = function ( nums ) {
            var len = nums.length,
                    res = [],
                    preProduct = nums[0],
                    lastProduct = nums[len - 1];
            for ( var n = 0; n < len; n++ ) {
                res[n] = 1;
            }
            for ( var i = 1; i < len; i++ ) {
                res[i] *= preProduct;
                res[len - i - 1] *= lastProduct;
                preProduct *= nums[i];
                lastProduct *= nums[len - i - 1];
            }
            return res;
        };

Log in to reply
 

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