Skip to content

Commit 42acf78

Browse files
Yug RajaniMohamad655
authored andcommitted
Solved "Product of Array Except Self" in Java.
Added solution to problem (https://leetcode.com/problems/product-of-array-except-self/) in Java. 18 / 18 test cases passed. Runtime: 1 ms Memory Usage: 48.2 MB
1 parent feba8fb commit 42acf78

File tree

1 file changed

+20
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)