Skip to content

Commit f0bb27b

Browse files
committed
Product of Array Except Self V2
1 parent efd1621 commit f0bb27b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

product-of-array-except-self/iam-edwin.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,24 @@ public int[] productExceptSelf(int[] nums) {
1616
}
1717
return result;
1818
}
19+
20+
public int[] productExceptSelfV2(int[] nums) {
21+
int[] result = new int[nums.length];
22+
23+
int curr = 1;
24+
result[0] = curr;
25+
for (int i = 1; i < nums.length; i++) {
26+
curr *= nums[i - 1];
27+
result[i] = curr;
28+
}
29+
30+
curr = 1;
31+
result[nums.length - 1] *= curr;
32+
for (int i = nums.length - 2; i >= 0; i--) {
33+
curr *= nums[i + 1];
34+
result[i] *= curr;
35+
}
36+
37+
return result;
38+
}
1939
}

0 commit comments

Comments
 (0)