Skip to content

Commit 6696959

Browse files
authored
Create 0238_Productofarray_except_self.java
1 parent dd3d408 commit 6696959

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

0238_Productofarray_except_self.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public int[] productExceptSelf(int[] nums) {
3+
int[] prefix = new int[nums.length];
4+
int[] suffix =new int[nums.length];
5+
6+
7+
prefix[0]=1;
8+
for(int i = 1 ; i<nums.length ; i++){
9+
prefix[i]= prefix[i-1] * nums[i-1];
10+
11+
}
12+
13+
14+
suffix[suffix.length-1]=1;
15+
16+
for(int i= nums.length - 2 ; i>=0 ; i--){
17+
suffix[i]=suffix[i+1] * nums[i+1];
18+
19+
}
20+
21+
22+
int[] ans = new int[nums.length];
23+
for(int i = 0 ; i<nums.length ; i++){
24+
ans[i]=prefix[i]* suffix[i];
25+
26+
27+
28+
}
29+
return ans;
30+
31+
}
32+
}

0 commit comments

Comments
 (0)