-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path238.cpp
More file actions
40 lines (39 loc) · 1.14 KB
/
Copy path238.cpp
File metadata and controls
40 lines (39 loc) · 1.14 KB
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
40
/*******************************************************************************
> File Name: 238.cpp
> Author: sillyplus
> Mail: oi_boy@sina.cn
> Created Time: Tue Mar 15 00:35:03 2016
*******************************************************************************/
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int zeroCount = 0;
vector<int> ret;
long long pd = 1;
for (int i = 0; i < nums.size(); i++) {
if (nums[i]) {
pd *= nums[i];
} else {
zeroCount++;
}
}
if (zeroCount >= 2) {
for (int i = 0; i < nums.size(); ++i) {
ret.push_back(0);
}
} else if (zeroCount == 1) {
for (int i = 0; i < nums.size(); ++i) {
if (nums[i]) {
ret.push_back(0);
} else {
ret.push_back(pd);
}
}
} else {
for (int i = 0; i < nums.size(); ++i) {
ret.push_back(pd / nums[i]);
}
}
return ret;
}
};