-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path238.cpp
More file actions
39 lines (37 loc) · 956 Bytes
/
238.cpp
File metadata and controls
39 lines (37 loc) · 956 Bytes
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
31
32
33
34
35
36
37
38
39
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
Solution() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
vector<int> productExceptSelf(vector<int>& nums) {
long long product = 1;
int countZero = 0;
for (auto& num : nums) {
if (!num) {
++countZero;
} else {
product *= num;
}
}
cout << product << endl;
vector<int> res;
for (auto& num : nums) {
if (countZero >= 2) {
return vector<int>(nums.size(), 0);
} else if (countZero == 1) {
if (num != 0) {
res.push_back(0);
} else {
res.push_back(product);
}
} else {
res.push_back(product / num);
}
}
return res;
}
};