Skip to content

Commit 0dcc0d8

Browse files
authored
feat:product-of-array-except-self
1 parent e7d61e3 commit 0dcc0d8

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var productExceptSelf = function(nums) {
6+
// 0์ด 2๊ฐœ ์ด์ƒ์ธ ๊ฒฝ์šฐ์—๋Š” ๋ฌด์กฐ๊ฑด 0
7+
if(nums.filter((num) => num === 0).length > 1) return Array.from({length:nums.length}).fill(0);
8+
9+
// 0์ด 1๊ฐœ์ธ ๊ฒฝ์šฐ์— ๋‚˜ํƒ€๋‚˜๋Š” ์ˆ˜
10+
const hasZero = nums.includes(0);
11+
12+
const allMatrix = nums.filter(Boolean). reduce((acc, cur) => (acc * cur),1);
13+
14+
return nums.map((num) => {
15+
if(hasZero){
16+
if(num !== 0) return 0;
17+
18+
return allMatrix
19+
}
20+
21+
return allMatrix / num
22+
});
23+
};

0 commit comments

Comments
ย (0)