Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c4c7f38

Browse files
committed
Update 238_Product_Of_Array_Except_Itself.java
1 parent c1c04ce commit c4c7f38

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
11
class Solution {
22
public int[] productExceptSelf(int[] nums) {
33
int[] result = new int[nums.length];
4-
4+
int product = 1;
5+
int zeroIdx = -1;
56
boolean hasZero = false;
6-
int product = 1, zeroIdx = 0;
77

88
for (int i = 0; i < nums.length; i++) {
99
if (nums[i] == 0) {
1010
if (hasZero) {
1111
return new int[nums.length];
1212
}
1313

14-
hasZero = true;
1514
zeroIdx = i;
16-
continue;
15+
hasZero = true;
16+
} else {
17+
result[i] = product;
18+
product *= nums[i];
1719
}
20+
}
1821

19-
result[i] = product;
20-
product *= nums[i];
22+
if (hasZero) {
23+
Arrays.fill(result, 0);
24+
result[zeroIdx] = product;
25+
return result;
2126
}
2227

2328
product = 1;
2429

2530
for (int i = nums.length - 1; i >= 0; i--) {
26-
if (nums[i] == 0) {
27-
continue;
28-
}
29-
3031
result[i] *= product;
3132
product *= nums[i];
3233
}
3334

34-
if (hasZero) {
35-
Arrays.fill(result, 0);
36-
result[zeroIdx] = product;
37-
}
38-
3935
return result;
4036
}
4137
}

0 commit comments

Comments
 (0)