Skip to content

Commit 5fc79a4

Browse files
authored
Create 238. Product of Array Except Self (#429)
2 parents b764e7f + 2527371 commit 5fc79a4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

238. Product of Array Except Self

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
void print(vector<int>& a, int n){
4+
for(int i=0; i<n; i++)
5+
cout<<a[i]<<',';
6+
cout<<endl;
7+
}
8+
vector<int> productExceptSelf(vector<int>& nums) {
9+
int n=nums.size();
10+
vector<int> a(n, 1);
11+
//a[i]=product of nums[k] over k>i
12+
for(int i=n-2; i>=0; i--){
13+
a[i]=nums[i+1]*a[i+1];
14+
}
15+
//b=product of nums[k] over k<j
16+
int b=1;
17+
for(int j=1; j<n; j++){
18+
b*=nums[j-1];
19+
a[j]*=b;
20+
}
21+
// print(a, n);
22+
return a;
23+
}
24+
};

0 commit comments

Comments
 (0)