Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fec4930

Browse files
Add solution to 238.
1 parent ecf72ea commit fec4930

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+
const prefixProduct = [1];
7+
for (let i = 1; i < nums.length; i++) {
8+
prefixProduct[i] = prefixProduct[i - 1] * nums[i - 1];
9+
}
10+
11+
const suffixProduct = [];
12+
suffixProduct[nums.length - 1] = 1;
13+
for (let i = nums.length - 2; i >= 0; i--) {
14+
suffixProduct[i] = suffixProduct[i + 1] * nums[i + 1];
15+
}
16+
17+
const product = [];
18+
for (let i = 0; i < nums.length; i++) {
19+
product[i] = prefixProduct[i] * suffixProduct[i];
20+
}
21+
22+
return product;
23+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /