Javascript use indexOf and lastIndexOf


  • -2
    D

    /**

    • @param {number[]} nums
    • @return {number[]}
      */

    var productExceptSelf = function(nums) {

    var total = nums.indexOf(0) !== nums.lastIndexOf(0) ? 0 : 1;
    var output = [];
    var hasZero = false;
    
    if (total === 1) {
        nums.forEach(function(self) {
            if (self !== 0) {
                total *= self;
            } else {
                hasZero = true;
            }
        });
    }
    
    nums.forEach(function(self) {
        if (total === 0) {
            output.push(0);
        } else {
            
            if (self !== 0) {
                output.push(hasZero ? 0 : total/self);
            } else {
                output.push(total);
            }
        }
    });
    
    return output;
    

    };


Log in to reply
 

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