-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path238.product-of-array-except-self.js
55 lines (51 loc) · 1.21 KB
/
238.product-of-array-except-self.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* @lc app=leetcode id=238 lang=javascript
*
* [238] Product of Array Except Self
*
* https://leetcode.com/problems/product-of-array-except-self/description/
*
* algorithms
* Medium (52.71%)
* Total Accepted: 203.7K
* Total Submissions: 385.9K
* Testcase Example: '[1,2,3,4]'
*
* Given an array nums of n integers where n > 1, return an array output such
* that output[i] is equal to the product of all the elements of nums except
* nums[i].
*
* Example:
*
*
* Input: [1,2,3,4]
* Output: [24,12,8,6]
*
*
* Note: Please solve it without division and in O(n).
*
* Follow up:
* Could you solve it with constant space complexity? (The output array does
* not count as extra space for the purpose of space complexity analysis.)
*
*/
/**
* @param {number[]} nums
* @return {number[]}
*/
function productExceptSelf(nums) {
if (nums.length === 0) return [];
if (nums.length === 1) return [0];
const result = [];
let productBefore = 1;
for (let i = 0; i < nums.length; i++) {
result.push(productBefore);
productBefore *= nums[i];
}
let productAfter = 1;
for (let j = nums.length - 1; j >= 0; j --) {
result[j] *= productAfter;
productAfter *= nums[j];
}
return result;
};