Skip to content

Commit

Permalink
feat: LC-0238: Product of Array Except Self
Browse files Browse the repository at this point in the history
add java solution for leetcode problem 328 -Product of Array Except Self
  • Loading branch information
galaumang committed Sep 29, 2023
1 parent c2a1d56 commit 6e7fb52
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dsalgo.array;

public class LC0238_ProductExceptSelf {
public int[] productExceptSelf(int[] nums) {
int l = nums.length;
int[] ans = new int[l];
ans[0]=1;
for (int i=1;i<l;i++)
ans[i] = nums[i-1] * ans[i-1];
int rightProduct=1;
for (int i=l-1; i>=0; i--){
ans[i] = ans[i] * rightProduct;
rightProduct *= nums[i];
}
return ans;
}
}

0 comments on commit 6e7fb52

Please sign in to comment.