Skip to content

Commit ebd571e

Browse files
committed
product of array except self solution
1 parent c2aa005 commit ebd571e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
vector<int> productExceptSelf(vector<int>& nums) {
4+
int zero = 0;
5+
int product = 1;
6+
vector<int> result;
7+
8+
for(int i = 0; i < nums.size(); i++){
9+
if(nums[i] == 0)
10+
zero++;
11+
else
12+
product *= nums[i];
13+
}
14+
15+
for(int i = 0; i < nums.size(); i++){
16+
if(zero > 1)
17+
result.push_back(0);
18+
else if(zero == 1){
19+
if(nums[i] == 0)
20+
result.push_back(product);
21+
else
22+
result.push_back(0);
23+
}else
24+
result.push_back(product / nums[i]);
25+
}
26+
27+
return result;
28+
}
29+
};

0 commit comments

Comments
 (0)