Skip to content

Commit caabec4

Browse files
dohee789dohee789
authored andcommitted
👔 product-of-array-except-self solution
1 parent bbf7872 commit caabec4

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
https://leetcode.com/problems/product-of-array-except-self/
3+
*/
4+
class Solution {
5+
public int[] productExceptSelf(int[] nums) {
6+
int n = nums.length;
7+
int[] result = new int[n];
8+
9+
result[0] = 1;
10+
for (int i = 1; i < n; i++) {
11+
result[i] = result[i - 1] * nums[i - 1];
12+
}
13+
14+
int right = 1;
15+
for (int i = n - 1; i >= 0; i--) {
16+
result[i] *= right;
17+
right *= nums[i];
18+
}
19+
20+
return result;
21+
}
22+
}
23+
24+
25+

0 commit comments

Comments
 (0)