forked from Nandinig24/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path238
30 lines (30 loc) · 733 Bytes
/
238
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
long long r = 1;
int c = 0;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] != 0)
r *= nums[i];
else if(nums[i]==0)
c++;
}
vector<int> a(nums.size()); // Initialize vector a with the same size as nums
for (int i = 0; i < nums.size(); i++) {
if (nums[i] != 0) {
if (c > 0)
a[i] = 0;
else {
a[i] = r / nums[i];
}
}
else if (nums[i] == 0) {
if (c > 1)
a[i] = 0;
else
a[i] = r;
}
}
return a;
}
};