-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path238.cpp
More file actions
44 lines (34 loc) · 900 Bytes
/
238.cpp
File metadata and controls
44 lines (34 loc) · 900 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
40
41
42
43
44
/*
* Written by Nitin Kumar Maharana
* nitin.maharana@gmail.com
*/
class Solution {
int countNumberOfZeroes(vector<int>& nums)
{
int count = 0;
for(int i: nums)
if(!i)
count++;
return count;
}
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> result(nums.size(), 0);
if(countNumberOfZeroes(nums) > 1)
return result;
int leftProd, rightProd;
leftProd = 1;
for(int i = 0; i < nums.size(); i++)
{
result[i] = leftProd;
leftProd *= nums[i];
}
rightProd = 1;
for(int i = nums.size()-1; i >= 0; i--)
{
result[i] = result[i] * rightProd;
rightProd *= nums[i];
}
return result;
}
};