Skip to content

Instantly share code, notes, and snippets.

@kennyxcao
Created October 19, 2017 16:41
Show Gist options
  • Save kennyxcao/5d77da695e7219582a381220525613c2 to your computer and use it in GitHub Desktop.
Save kennyxcao/5d77da695e7219582a381220525613c2 to your computer and use it in GitHub Desktop.
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function(nums) {
var output = nums.slice().fill(1);
var temp = 1;
for (var i = 0; i < nums.length; i++) {
output[i] = temp;
temp *= nums[i];
}
temp = 1;
for (var i = nums.length - 1; i >= 0; i--) {
output[i] *= temp;
temp *= nums[i]
}
return output;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment