Skip to content

Commit c8d979a

Browse files
committed
feat: product of array except self
1 parent 5b24489 commit c8d979a

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+
class Solution {
2+
public int[] productExceptSelf(int[] nums) {
3+
int len = nums.length;
4+
int[] res = new int[len];
5+
int num = 1;
6+
7+
for (int i = 0; i < len; i++) {
8+
res[i] = num;
9+
num *= nums[i];
10+
}
11+
num = 1;
12+
for (int i = len -1; i>=0; i--) {
13+
res[i] *= num;
14+
num *= nums[i];
15+
}
16+
17+
for (int i = 0; i < len; i++) {
18+
System.out.print( res[i] + " ");
19+
}
20+
21+
return res;
22+
}
23+
}

0 commit comments

Comments
 (0)