-
Notifications
You must be signed in to change notification settings - Fork 64
/
sol.cpp
51 lines (47 loc) · 960 Bytes
/
sol.cpp
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
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
vector<int> productExceptSelf(vector<int> &nums)
{
vector<int> answer;
int m = 1;
int m1 = 1;
int count = 0;
for (int i = 0; i < nums.size(); i++)
{
answer.push_back(nums[i]);
if (nums[i] != 0)
{
m = m * nums[i];
m1 = m1 * nums[i];
}
else
{
m = m * nums[i];
count++;
}
}
for (int i = 0; i < nums.size(); i++)
{
if (count > 1)
{
answer[i] = 0;
}
else if (nums[i] == 0)
{
answer[i] = m1;
}
else
answer[i] = m / nums[i];
}
return answer;
}
};
int main()
{
// call the fn here
return 0;
}