We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e7d61e3 commit 0dcc0d8Copy full SHA for 0dcc0d8
โproduct-of-array-except-self/hozzijeong.jsโ
@@ -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