|
3 | 3 | * @return {number[]}
|
4 | 4 | */
|
5 | 5 | const productExceptSelf = (nums) => {
|
6 |
| - let map = new Map(), defaultFactor = 1; |
| 6 | + if (1 === nums.length) return [0]; |
7 | 7 |
|
8 |
| - console.log(map) |
| 8 | + let factorList = [], defaultFactor = 1; |
9 | 9 |
|
10 | 10 | for (let i = 0; i < nums.length; i++) {
|
11 |
| - map = new Map( |
12 |
| - Array.from(map, ([key, value]) => |
13 |
| - [key, value * nums[i]] |
14 |
| - ) |
15 |
| - ) |
16 |
| - |
17 |
| - map.set(nums[i], defaultFactor); |
| 11 | + factorList[i] = defaultFactor; |
18 | 12 | defaultFactor *= nums[i];
|
19 | 13 | }
|
20 | 14 |
|
21 |
| - console.log(map) |
22 |
| - console.log(Array.from(map, ([key, value]) => Math.abs(value))) |
| 15 | + defaultFactor = 1; |
| 16 | + |
| 17 | + // [1, 4, 12, 24] |
| 18 | + for (let j = 0; j < nums.length; j++) { |
| 19 | + console.log(factorList[nums.length - (j + 1)]); |
| 20 | + factorList[nums.length - (j + 1)] *= defaultFactor; |
| 21 | + defaultFactor *= nums[nums.length - (j + 1)]; |
| 22 | + } |
23 | 23 |
|
24 |
| - return; |
| 24 | + console.log(factorList) |
| 25 | + // console.log(Array.from(map, ([key, value]) => Math.abs(value))) |
| 26 | + // |
| 27 | + // return; |
25 | 28 | };
|
26 | 29 |
|
| 30 | + |
| 31 | + |
27 | 32 | // productExceptSelf([1, 2, 3, 4])
|
28 |
| -productExceptSelf([-1, 1, 0, -3, 3]) |
| 33 | +// productExceptSelf([1, 2, 3, 4, 2]) |
| 34 | +// productExceptSelf([-1, 1, 0, -3, 3]) |
| 35 | +// productExceptSelf([0, 0]) |
| 36 | +productExceptSelf([0]) |
0 commit comments